Is it possible to use mysqli_fetch_object with a prepared statement

前端 未结 3 1457
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 15:22

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 c

3条回答
  •  悲&欢浪女
    2020-12-03 16:11

    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:

    $result = $stmt->result_metadata();
    

    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.

提交回复
热议问题