views:

61

answers:

5

Hi,

Does anyone have a PHP function that for example if a mysql_error() happens due to well a MySQL error it will not output it?

By not output it I mean rather it not show the error on live site as I will use it with another function I have that would work a treat if I had a MySQL error function.

I am just finding it so annoying as I do it like this currently:

$q = mysql_query("SELECT * FROM something");

// if error occurs
if(!$q) {
   echo 'Error' . mysql_error();
} else {
  // else no errors so continue
}

On some of my webpages see I have several queries in a script and I would like to just be able to include the function at the bottom of all my PHP code and if a MySQL error occurs anywhere in my script for the function to catch the error instead of me doing multiples of the code I quoted above.

That way I can save myself a lot of unnecessary work and implement it with my email error function.

+3  A: 

One way is to write your own function, say sql_query, which looks something like this:

function sql_query($sql) {
    $q = mysql_query($sql);
    // if error occurs
    if(!$q) {
       //your error handling code
    } else {
       return $q;
    }
}

You then use this function wherever you want to do a sql query.

A much better way is to use PDO and the exceptions etc in that.

Jords
PDO? not sure what you mean. This looks like what i am looking thank you Jords :) I guess i would use this like: sql_query($sql) $sql variable being my code that wants querying ? and if it fails it will obviously do my error handling code
PHPLOVER
Yeah. PDO is php data objects - google PHP PDO and you will find it. It's an DB abstraction layer that is quite nice and you can then use exceptions to handle this sort of thing better, and you also get prepared statements which are very nice.
Jords
+1  A: 

Make or use a DB abstraction layer.

Use Exceptions and catch all of them. Then you have a "setting" to turn on or off the display of errors or better yet, log them to file.

sims
A: 

I think i don't understand what you need.

If you want to 'hide' error from being print on the page, you just can use de '@' prefix:

$q = @mysql_query("SELECT * FROM something");

With the '@', errors are suppressed to the end-user.

HarzIce
Error suppression should be avoided! You should fix the problem, not hide it.
Russell Dias
Errors can be due to an error that does not relate to the query itself like syntax errors. Also not displaying them on a live site is what should be done as it can disclose to much information specially to an attacker, hence why i said about integration with my email error function so i will be notified of exact error yet user will get an error that you specify.
PHPLOVER
@PHPLOVER, @ is still a poor way to deal with this. As stated, use exceptions and catch them and log them to file so the visitor does not see them.
sims
A: 

see here : http://stackoverflow.com/questions/2831263/is-it-best-practice-to-try-catch-my-entire-php-code-or-be-as-specific-as-possi/2839873#2839873

f00
I really don't understand OOP as of yet so i would not know how to use that code, but thanks for posting thou, still appreciated of course.
PHPLOVER
A: 

The most robust solution is to probably use trigger_error(). This way you can tie into PHP's error handling system which is already prepared to deal with development and production environments.

$querySql = 'SELECT * FROM `foo`';
$queryResult = mysql_query($querySql);

if (!$queryResult) {
    trigger_error('Unable to execute query: ' . $querySql, E_USER_NOTICE);
}

Of course, the most convenience would be to make a decorating function for mysql_query() that automatically triggered the error. The DRY principle always applies.

erisco
Hi, E_USER_NOTICE what is this? this is new to me plus trigger_error never heard of that before either. Thanks
PHPLOVER
also to add to the above, does it display a error message but an error that does not reveal and information about the database etc, which is obviously good on a live site to avoid giving out information about the query etc or am i wrong in thinking that?
PHPLOVER
Could anyone answer the question above in my reply? really interested to know ? thanks
PHPLOVER
Use http://php.net to find answers to those questions PHPLOVER.
erisco
Hi erisco, for some reason i types it in but cannot find anything that gives a good description on what it means exactly, i also googled searched, i just don't understand what they mean exactly it appears to not be very descriptive.
PHPLOVER