Fm_sql_execute
Hi,
I'm trying to use fm_sql_execute to update a record that doesn't exists in fm. Nothing to complicated relay
$sql = 'UPDATE Category SET name='test' WHERE id=12;
$result = fm_sql_execute($sql);
regardless if id exists or not $result will be true? I need to know if the update was successful or not because if the record doesn't exists I want to create it.
As far as I can see there is no support for REPLACE INTO command?
Thanks, I worked it out in a
Thanks, I worked it out in a similar way.
fm_sql_execute
Hello Tomas,
I'm afraid I don't know of any good solution for this. I think the only way to solve this is to first do a SELECT to see if the record exists and then branch appropriately with an UPDATE or INSERT. The FileMaker ODBC and JDBC guide (published by FileMaker) is the best reference in regards to which SQL statements are supported.
Here's an example:
// Store id$id = 12;
// Does the record exist?
$sql = "SELECT id from Category WHERE id=$id";
$result = fm_sql_select($sql);
// Error?
$error = fm_get_last_error();
if ($error != 0) {
die("A query error occurred: $error");
}
// Branch for UPDATE/INSERT
if (empty($result)) {
$sql = "INSERT INTO Category (name, id) VALUES ('test', $id)";
$action = "inserted";
} else {
$sql = "UPDATE Category SET name='test' WHERE id=$id";
$action = "updated";
}
// Execute query
$result = fm_sql_execute($sql);
// Error?
$error = fm_get_last_error();
if ($error != 0) {
die("A query error occurred: $error");
}
echo "The record was sucessfully $action.";
Regards,
Micah