准备好的陈述错误

时间:2011-11-12 11:09:21

标签: php mysql mysqli prepared-statement fetch

在调试过程中,我注意到脚本在$stmt->fetch() or 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();

1 个答案:

答案 0 :(得分:2)

问题是如果SQL语句没有检索到行,$stmt->fetch()将返回布尔值FALSE。因此,您必须检查行数:

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