Tural Ali
Tural Ali

Reputation: 23290

Prepared statements error

During debug process, I noticed that script dies on $stmt->fetch() or die($stmt->error); line. There is no error in PHP log. Also it doesn't echo any error. Do you see any wrong piece in code?

$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();

Upvotes: 2

Views: 1307

Answers (1)

Alessandro Desantis
Alessandro Desantis

Reputation: 14343

The problem is that $stmt->fetch() returns boolean FALSE if no rows are retrieved by the SQL statement. So, you have to check for the number of rows:

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

Upvotes: 2

Related Questions