Go Back   Webforumz.com > Resources > Tips & Tricks > PHP & MySQL

Notices


Stop MySQL Displaying Error Messages

Stop MySQL Displaying Error Messages

Published: Feb 1st, 2008
Your database goes down and suddenly your website is full of hideous error messages. What a turn off!



There is a very easy solution to this problem. Obviously, the connection should never go wrong, but we all know that a simple typo can creep in and suddenly you are left with a website displaying horrible errors, which is a large turn off to visitors, and/or potential customers.

The trick is to place '@' in front of the mysql_query. For example:

$sql = mysql_query("SELECT id, text, author FROM table"); 

Becomes:

$sql = @mysql_query("SELECT id, text, author FROM table");

The '@' simply tells the database that, if there is a failure, to do nothing. However, the chances are you will still want an error message, so we can add in an 'if' statement:

$sql = @mysql_query("SELECT id, text, author FROM table");
if(!$sql) {
exit('Error fetching data');
}

One final trick is the use of 'exit'. This tells the code to not 'use' any PHP after that line. So, any PHP below the above, will not be used if there is an error fetching the data, and also the PHP will display the text 'Error Fetching Data' if all goes wrong. You could change this to something like 'Error. Please contact Web Master'.




Members's Comments & Rating
No Comments. Be the first to comment on and rate this resource.

Author

Jack Franklin
Jack is 15 years old and lives happily in Cornwall, England. Web Design is his passion and he works using xHTML, CSS and PHP to code his websites, with the odd bit of Javascript now and then. In his spare time, Jack enjoys playing Football, rugby, tennis and snooker (but not all at the same time). Jack can be contacted through his online E-Portfolio.

View more articles from Jack Franklin

All times are GMT. The time now is 08:12.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43