Jump to content

Recommended Posts

It's early and I'm probably just missing something obvious, but it's confusing me...

	if (! $stmt = $db->prepare($sql)) {
		print_r($db);
		//echo "Error=". $db->error."<br />";
		//$msg = "SQL failed -".$db->error;
		print_r($db);
	}

The above code is intended to trap a failure in a prepare and give me something meaningful out of it.  BUT the output (abbreviated) I get from this is...

mysqli Object ( ... [connect_errno] => 0 [connect_error] => [errno] => 1146 [error] => Table 'warehouse.BinType1' doesn't exist [error_list] => Array ( ) ...
mysqli Object ( ... [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) ...

So even simply doing a print_r on the $db object is causing the error to be cleared.

Even stepping through on debug shows that when I arrive at the (commented out) echo statement or the second print_r - the values have been cleared.

The $db object is from a straight forward new mysqli() and it works for most things I've tried so far.

So I thought I'd ask for some enlightenment and in the meanwhile I'll fix the SQL causing the error :-/

Thanks

Link to comment
https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/
Share on other sites

It's possible that the act of dumping $db will reset the errno, as "all functions that have to ask the server for information reset [the errno] if they succeed", and the mysqli class has a number of dynamic properties.

 

Try dumping just $db->errno.

Hmmm... I've changed it to

print_r($db->errno);
$msg = "SQL failed -".$db->error;

And that now gives me 0 as the errorno (even though in the Variable panel I can see a value of 1146 at the point of executing that statement.  Once I've stepped over the print_r line, the values are reset.

 

:happy-04: Think I may have found the culprit - the very helpful debugging.  If I run the code without debugging, it works as expected, through debugging must be doing as you've said and causing some side effects by getting statuses or something.

 

I use Eclipse and XDebug which although is very useful at times, I must remember that database objects may be affected!

Pro tip: Instead of trying to build your own error mechanism, just enable exceptions.

<?php

$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

$databaseConnection = new mysqli(...);

This will automatically trigger an exception whenever something fails. No need to check return values, no need to pull error codes out of attributes, no need to assemble your own messages.

On the subject of how to catch/manage errors (exceptions or any combination) - is there a common way in which a PHP applications can trap, log errors and then present the user with a 'Something went wrong' screen, or is this down to individual implementations?  

I can see the problem here is that this relies on all of the potential areas for errors being complete prior to building any screen output - or does it? Is this something a framework would have built in (am starting to look into Lavravel).

 

(Sorry for asking so many questions as I'm trying to find out as much as possible from people who actually do these things)

PHP and webservers have built-in error handling features. PHP can log error messages and emit a 500 status code for fatal errors. This status code can then be caught by the webserver to display an error page (preferrably a basic static HTML page). See this overview.

 

If you want a fancier mechanism, you have to implement it yourself or use whatever your framework offers. Be aware that error handling in PHP is quite tricky. There are both exceptions and classical errors, a lot of errors cannot be caught with a simple error handler, and some errors can happen before the application even runs. The only reliably approach I'm aware of is to auto-prepend a script which registers a shutdown handler (as described in the above overview).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.