Jump to content



Photo

Prepared Statements And Error Handling


  • Please log in to reply
3 replies to this topic

#1 masterlayouts

masterlayouts

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 12 February 2012 - 6:31 AM

The custom error handler function as defined in your book:

function my_error_handler($e_number, $e_message, $e_file, $e_line, $e_vars) {
$message = "An error occured in script '$e_file' on line $e_line: $e_message\n<br />";
$message .= "Date/Time: " . date('d-j-Y H:i:s') . "\n<br />";
$message .= "<pre>" . print_r($e_vars, 1) . "</pre>\n</p>";

if (!LIVE) {
echo $message;
}
else {
@mail(ADMINEMAIL, 'Site Error', $message);

if($e_number != E_NOTICE) {
echo "<div id=\"error\">A system error occurred.</div>";
}
}
}

set_error_handler('my_error_handler');


It may handle errors as they happen in something like this one:

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " .mysqli_error($dbc));

My question is: How about the prepared statments? Can be something like:

mysqli_stmt_execute($stmt) or trigger_error("Query: $q\n<br />MySQL Error: " .mysqli_error($dbc));

or should we use mysqli_stmt_error($stmt) and mysqli_stmt_errno($stmt)? Or maybe it is not necessary to do anything? Bottom line: how do we handle errors when using prepared statements?

Thank you.
  • 1

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,714 posts
  • LocationState College, PA (USA)

Posted 13 February 2012 - 7:21 PM

The short answer is that you can use mysqli_error(), but I'll write something more formal up and share that with you in a couple of days.
  • 0

#3 masterlayouts

masterlayouts

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 24 February 2012 - 9:54 AM

mysqli_stmt_execute($stmt) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_stmt_errno($stmt) . ": " . mysqli_stmt_error($stmt));

This seems to work fime. Do I have to modify something else in your function?
  • 0

#4 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,714 posts
  • LocationState College, PA (USA)

Posted 24 February 2012 - 10:12 PM

No, that looks fine, although the $q variable may not be too useful here.
  • 0