NigelRel3 Posted March 17, 2017 Share Posted March 17, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/ Share on other sites More sharing options...
requinix Posted March 17, 2017 Share Posted March 17, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544285 Share on other sites More sharing options...
NigelRel3 Posted March 17, 2017 Author Share Posted March 17, 2017 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. 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! Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544286 Share on other sites More sharing options...
requinix Posted March 17, 2017 Share Posted March 17, 2017 Yup, that's it. You can still use debugging, but you cannot do anything that would cause the properties in the mysqli object to be evaluated. Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544287 Share on other sites More sharing options...
Jacques1 Posted March 17, 2017 Share Posted March 17, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544289 Share on other sites More sharing options...
NigelRel3 Posted March 17, 2017 Author Share Posted March 17, 2017 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) Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544298 Share on other sites More sharing options...
Jacques1 Posted March 17, 2017 Share Posted March 17, 2017 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). Quote Link to comment https://forums.phpfreaks.com/topic/303471-error-code-is-dissapearing/#findComment-1544304 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.