tags:

views:

7

answers:

1

I'm a bit not understanding - on update, insert, delete queries result_metadata returns false? Because:

$meta = $stmt->result_metadata();
if($meta === false){
    throw new Exception(...));
}

throws the exception when i try to run update, insert or delete query. But it should be false only on error. (from php manual)

+1  A: 

The function is intended to return data about the data you've just made a query about (metadata!). The action of inserting rows, updating rows, and deleting rows usually doesn't actually return a result set.

(At least not in MySQL. In certain other databases, like Postgres, there's the RETURNING clause...)

From the comments on the mysqli_stmt_result_metadata manual page:

If result_metadata() returns false but error/errno/sqlstate tells you no error occurred, this means your query is one that does not produce a result set, i.e. an INSERT/UPDATE/DELETE query instead of a SELECT query.

This is stated in the documentation where it says "If a statement passed to mysqli_prepare() is one that produces a result set, mysqli_stmt_result_metadata() returns the result object", but it might not be clear to everyone what this entails exactly.

Charles
Oh thank you, but then how to detect whether there was error when calling the result metadata function or it was insert/update/delete query?
Richards
[Check for an error](http://us2.php.net/manual/en/mysqli-stmt.error.php) or poke at the [SQLSTATE](http://us2.php.net/manual/en/mysqli-stmt.sqlstate.php) -- if they contain an error, there's your error. If they don't contain an error, there was no error.
Charles