SELECT statement with fetch_array in mysqli prepared statements

mysql, mysqli, php

Solution

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
}

Problem

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()) { // ... } ```

Original source