sixli
sixli

Reputation: 303

prepared statements fetch_assoc php mysqli

I'm having problems with listing out comments with prepared statements. Any ideas?

Here is my code:

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

Upvotes: 5

Views: 7433

Answers (2)

Fabio
Fabio

Reputation: 23490

You have an error in your script. You are using mysqli_fetch_assoc while you have to use fetch(). The error is here

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

So you should use instead

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

You can check documentation here

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157864

[For some unknown (and really weird) reason] you cannot use fetch_assoc on mysqli_stmt object.
you need to get mysqli result resource first with mysqli_get_result().

Also please name your variables consistently. Mysqli statement has nothing to do with your comments and knows nothing of them, nor contain them. It's just a mysqli statement object.

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

Though you never can tell whether this function would be available with mysqli or not.

Upvotes: 8

Related Questions