mysqli 准备语句中带有 fetch_array 的 SELECT 语句

2021-12-25 00:00:00 php mysql mysqli

我总是发现编写 MySQLi 准备好的语句很困难,因为许多函数的工作方式与旧方式不同.现在我正面临一个关于 fetch_array() 的问题.

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

推荐答案

您正在尝试通过

$result = $stmt->execute();

事实并非如此.因为 execute 只会返回一个布尔值.

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

喜欢就好.

$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
}

相关文章