5

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'];
    }
}
Sjon
  • 4,605
  • 6
  • 24
  • 42
sixli
  • 293
  • 3
  • 14

2 Answers2

8

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

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
  • If someone is also getting problem with get_result() please refer to this: 1. in case of fatal error install mysqlnd: http://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result 2. In case of no result remove line: "$stmt->store_result()" (rest unchanged) – suz Nov 07 '15 at 08:51
0

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

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
Fabio
  • 21,516
  • 12
  • 49
  • 63