TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Preventing mysqli errors from showing (http://www.talkphp.com/absolute-beginners/4337-preventing-mysqli-errors-showing.html)

vixxy 05-18-2009 03:50 AM

Preventing mysqli errors from showing
 
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'mysql7.......' (111) in /home/....t/defaults/co.php on line 8
and
Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in /home/..../co.php on line 18

Anyway how i can prevent these errors from showing up my on my site? In my file (co.php), i have this:
PHP Code:

mysqli_report(MYSQLI_REPORT_OFF);
$dbcon = new mysqli($server,$user,$pass,$db); 

I have tried these various codes:
PHP Code:

if (!$dbcon) {
    die(
'Server Error');
}
if (
$dbcon->ping()) {
 die(
'Server Problem');
}
if (
mysqli_connect_error()) { die('Connect failed1'); } 
if (
mysqli_connect_errno()) { die('Connect failed2'); } 

and yet, none of them is currently able to make mysqli shut up when it cant connect

Hightower 05-18-2009 07:22 AM

I have a custom error handler setup for my sites. When it is in development it will show the error in full, but when the site is live it displays a "gentle" message (or none at all if you wish), and instead it sends an email alerting me to the fact that an error occured.

Take a look at this thread: http://www.talkphp.com/absolute-begi...html#post24193

There is a little bit more to it than what you can see there, but if this is the kind of thing you are looking for give me a nod and I will post the full code for you.

vixxy 05-18-2009 01:13 PM

I still dont understand it that much. If i remember correctly, php exceptions have do not work well with catching mysql errors

sketchMedia 05-18-2009 01:16 PM

Thats because its not throwing an exception, its producing a warning.

This may be of some use to you: http://uk.php.net/set_error_handler

as an alternative, on the production you may want to run with :
PHP Code:

error_reporting(0); 

That will stop any warnings etc from being displayed.

Wildhoney 05-18-2009 01:19 PM

You can suppress errors, although it's not recommended, by adding an @ symbol in front of the function:

php Code:
@ping();

The better way is to, as Hightowers suggested, set up different modes of error displaying for different users. For yourself, the developer, you want to see these warnings, but users don't.

To not show warnings you can do:

php Code:
error_reporting(0);
/* Or... */
ini_set('display_errors', 'off');

Note to self: Type quicker next time so that sketchMedia doesn't beat me to it. Git!

sketchMedia 05-18-2009 01:26 PM

Quote:

Note to self: Type quicker next time so that sketchMedia doesn't beat me to it. Git!
:-P

A good point in your post:
Quote:

You can suppress errors, although it's not recommended, by adding an @ symbol in front of the function:
Indeed its not recommended, having to grep through code to find those pesky things when your script is just quietly falling over is a tedious task at best!

allworknoplay 05-18-2009 02:19 PM

Yeah, plus from what I hear it slows down your script.

sketchMedia 05-18-2009 02:38 PM

Yes, i believe its because internally it converts this:

e.g.
PHP Code:

@someFunction(); 

to this:
PHP Code:

error_reporting(0);
someFunction();
error_reporting(WHATEVER IT WAS SET TO BEFORE); 



All times are GMT. The time now is 11:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0