php - 准备好的语句错误

标签 php mysql mysqli prepared-statement fetch

在调试过程中,我注意到脚本死于 $stmt->fetch() 或 die($stmt->error); 行。 PHP 日志中没有错误。它也不会回显任何错误。您是否看到代码中有任何错误的部分?

$stmt = $db->prepare("SELECT e.msg_subject AS subject, e.msg_text AS text, u.fname AS firstname, u.lname AS lastname FROM emails AS e, users AS u WHERE e.msg_status=? AND e.msg_type=? AND u.id=?") or die($db->error);
    $stmt->bind_param("isi", $status, $type, $userid) or die($stmt->error);
    $stmt->execute() or die($stmt->error);
    $stmt->bind_result($subject, $text, $firstname, $lastname) or die($stmt->error);
    $stmt->fetch() or die($stmt->error);
    $stmt->close();

最佳答案

问题在于,如果 SQL 语句未检索到任何行,$stmt->fetch() 将返回 bool 值 FALSE。因此,您必须检查行数:

if ($stmt->num_rows > 0) {
    $stmt->fetch() or die($stmt->error);
}

关于php - 准备好的语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8104065/

相关文章:

php - 如何修复未找到表定义的错误?

mysql - 如何等待 docker-entrypoint-initdb.d 在 Shell Script/Bash 中加载数据库?

php - 使用 php 表单将值存储到数据库中

mysql - 错误 : Handshake inactivity timeout in Node. js v6.9.1 和 MySQL

php - 不知道如何为我的两个类创建全局 Mysqli 连接

php - 我可以在 PHP 中混合使用 MySQL API 吗?

javascript - X 和 Y 坐标的简单笛卡尔平面,原点位于中心

php - 将 JSON 中的值插入数据库

php - 实现facebook风格的 "unlike"函数

mysql - 更新内部连接卡住