【问题标题】:prepared statements fetch_assoc php mysqli准备好的语句 fetch_assoc php mysqli
【发布时间】:2013-06-09 19:38:50
【问题描述】:

我在列出带有准备好的语句的 cmets 时遇到问题。有什么想法吗?

这是我的代码:

$fetchComments = $cnx -> prepare("SELECT comment FROM comments WHERE video_id=? LIMIT 1");
$fetchComments -> bind_param('s', $row['id']);
$fetchComments -> execute();
$fetchComments -> store_result();
$fetchComments -> bind_result($vid_comment);
if ($fetchComments -> num_rows > 0) {
    whike ($row = mysqli_fetch_assoc($vid_comment)){
    echo $row['comment'];
    }
}

【问题讨论】:

  • $row['id'] 来自哪里?
  • 问题是……?
  • 语法错误whike应该是while
  • @Fabio $row['id'] 来自前一个 while 循环,该循环在帖子中循环。在那个循环中,我试图打印出 cmets。语法错误不在源代码中(就在这里)。我得到的错误是带有while循环的行:警告:mysqli_fetch_assoc()期望参数1是mysqli_result,在中给出的null
  • while ($fetchComments ->fetch()) { echo $vid_comment

标签: php mysqli prepared-statement


【解决方案1】:

[出于某种未知(而且非常奇怪)的原因] 你不能在 mysqli_stmt 对象上使用 fetch_assoc。
您需要先使用 mysqli_get_result() 获取 mysqli 结果资源。

另外一致地命名您的变量。 Mysqli 语句与您的 cmets 无关,对它们一无所知,也不包含它们。它只是一个 mysqli 语句对象。

$stmt->execute();
$res = $stmt->get_result(); // here you go
while ($row = mysqli_fetch_assoc($res)) {
    echo $row['comment'];
}

虽然你永远无法判断这个功能是否适用于 mysqli。

【讨论】:

  • 如果有人也遇到 get_result() 的问题,请参考: 1. 如果出现致命错误,请安装 mysqlnd:stackoverflow.com/questions/8321096/… 2. 如果没有结果删除行:“$stmt- >store_result()"(保持不变)
【解决方案2】:

您的脚本中有错误。您使用的是mysqli_fetch_assoc,而您必须使用fetch()。错误在这里

while ($row = mysqli_fetch_assoc($vid_comment)){

所以你应该改用

while ($fetchComments ->fetch()) {
   echo $vid_comment 
}

您可以查看documentation here

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2016-07-22
    • 2012-12-01
    相关资源
    最近更新 更多