0

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
PHPLOVER
  • 7,047
  • 18
  • 37
  • 54
  • 1
    I can't wait till you have a database error you need to debug because the page is loading but there's no data... – OMG Ponies Sep 06 '10 at 01:14
  • I doubt that code would pass the tokenizer - you are missing a period (for concatenating the string and `mysql_error()` return) – alex Sep 06 '10 at 01:14
  • I just wrote that code quickly a second thats not my actual code i copied from one of my webpages, hence a two typos, no worries there. I just quickly typed it but have rectified it for the sake of people not needing to mention it. – PHPLOVER Sep 06 '10 at 01:16

4 Answers4

3

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
  • 1,855
  • 3
  • 18
  • 28
  • 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 Sep 06 '10 at 02:19
  • 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 Sep 06 '10 at 11:51
1

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.

d-_-b
  • 6,555
  • 5
  • 40
  • 58
1

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
  • 14,154
  • 2
  • 40
  • 45
  • Hi, E_USER_NOTICE what is this? this is new to me plus trigger_error never heard of that before either. Thanks – PHPLOVER Sep 06 '10 at 02:18
  • 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 Sep 06 '10 at 02:26
  • Could anyone answer the question above in my reply? really interested to know ? thanks – PHPLOVER Sep 06 '10 at 07:56
  • 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 Sep 06 '10 at 18:43
0

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.

Esselans
  • 1,540
  • 2
  • 24
  • 44
  • 1
    Error suppression should be avoided! You should fix the problem, not hide it. – Russell Dias Sep 06 '10 at 01:36
  • 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 Sep 06 '10 at 02:17
  • @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. – d-_-b Sep 06 '10 at 05:53