准备好的语句fetch_assoc php mysqli

时间:2013-06-09 19:38:51

标签: php mysqli prepared-statement

我在准备好的陈述中列出评论时遇到了问题。有什么想法吗?

这是我的代码:

$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'];
    }
}

2 个答案:

答案 0 :(得分:7)

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

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

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

虽然你永远无法判断这个函数是否可用于mysqli。

答案 1 :(得分:1)

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

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

所以你应该使用

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

您可以查看documentation here