Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT statement with fetch_array in mysqli prepared statements

Tags:

php

mysql

mysqli

I always find it difficult to write MySQLi prepared statements, because many functions work differently than in the old way. Right now I am facing a problem regarding fetch_array().

$stmt = $db->prepare("SELECT category_id FROM post_items Where user_id = ?");
$stmt->bind_param('i', $userid);
$result = $stmt->execute();
while ($row = $result->fetch_array()) {
    // ...
}
like image 683
user3277912 Avatar asked Dec 11 '25 14:12

user3277912


1 Answers

you are trying to fetch the results by

$result = $stmt->execute();

which is not the case. as execute will return you only a boolean value.

do it like.

$stmt = $db->prepare("SELECT category_id FROM post_items Where user_id = ?");
$stmt->bind_param('i', $userid);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
        //result is in row
}
like image 171
Mithun Satheesh Avatar answered Dec 13 '25 02:12

Mithun Satheesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!