All the examples I see using mysqli_fetch_object use
mysql_query(), I cannot get it to work with prepared statements. Does anyone know what is wrong with this code snippet, as fetch_object returns null.
$sql = "select 1 from dual"; printf("preparing %sn", $sql); $stmt = $link->prepare($sql); printf("prepare statement %sn", is_null($stmt) ? "is null" : "created"); $rc = $stmt->execute(); printf("num rows is %dn", $stmt->num_rows); $result = $stmt->result_metadata(); printf("result_metadata %sn", is_null($result) ? "is null" : "exists"); $rc = $result->fetch_object(); printf("fetch object returns %sn", is_null($rc) ? "NULL" : $rc); $stmt->close();
The output is:
preparing select 1 from dual prepare statement created num rows is 0 result_metadata exists fetch object returns NULL
I don't believe the interface works like that.
Going by the documentation and examples (http://www.php.net/manual/en/mysqli.prepare.php) it seems that $stmt->execute() does not return a resultset, but a boolean indicating success / failure (http://www.php.net/manual/en/mysqli-stmt.execute.php). To actually get the result, you need to bind variables to the resultset (aftere the execute call) using $stmt->bind_result (http://www.php.net/manual/en/mysqli-stmt.bind-result.php).
After you did all that, you can do repeated calls to $stmt->fetch() () to fill the bound variables with the column values from the current row. I don't see any mention of $stmt->fetch_object() nor do I see how that interface could work with a variable binding scheme like described.
So this is the story for "normal" result fetching from mysqli prepared statments.
In your code, there is something that I suspect is an error, or at least I am not sure you intended to do this. You line:
assignes the resultset metadata, which is itself represented as a resultset, to the $result variable. According to the doc (http://www.php.net/manual/en/mysqli-stmt.result-metadata.php) you can only use a subset of the methods on these 'special' kinds of resultsets, and fetch_object() is not one of them (at least it is not explicitly listed).
Perhaps it is a bug that fetch_object() is not implemented for these metadata resultsets, perhaps you should file a bug at bugs.mysql.com about that.