是否可以将mysqli_fetch_object与已准备好的语句一起使用 [英] Is it possible to use mysqli_fetch_object with a prepared statement

查看:65
本文介绍了是否可以将mysqli_fetch_object与已准备好的语句一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mysqli_fetch_object看到的所有示例都使用mysql_query(),我无法使其与准备好的语句一起使用.没有人知道此代码段的错误之处,因为fetch_object返回null.

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 %s\n", $sql);
$stmt = $link->prepare($sql);
printf("prepare statement %s\n", is_null($stmt) ? "is null" : "created");
$rc = $stmt->execute();
printf("num rows is %d\n", $stmt->num_rows);
$result = $stmt->result_metadata();
printf("result_metadata %s\n", is_null($result) ? "is null" : "exists");
$rc = $result->fetch_object();
printf("fetch object returns %s\n", is_null($rc) ? "NULL" : $rc);
$stmt->close();

输出为:

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.

按文档和示例进行操作( http://www.php. net/manual/en/mysqli.prepare.php )似乎$ stmt-> execute()不会返回结果集,而是一个指示成功/失败的布尔值( http://www.php.net/manual/en/mysqli-stmt.bind-result.php ).

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).

完成所有这些操作后,您可以重复调用$ stmt-> fetch()()以用当前行中的列值填充绑定的变量.我没有提到$ stmt-> fetch_object(),也没有看到该接口如何与如上所述的变量绑定方案一起工作.

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.

这就是从mysqli准备的陈述中获取正常"结果的故事.

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();

将结果集元数据(其本身表示为结果集)分配给$ result变量.根据文档( http://www.php.net /manual/zh-CN/mysqli-stmt.result-metadata.php ),您只能在这些特殊"类型的结果集上使用方法的子集,而fetch_object()不是其中之一(至少它是其中之一)没有明确列出).

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).

可能是未为这些元数据结果集实现fetch_object()的错误,也许您应该在 bugs处提交错误. .mysql.com .

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.

这篇关于是否可以将mysqli_fetch_object与已准备好的语句一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆