Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register

Go Back   WebForumz.com > Resources > Tips & Tricks > PHP & MySQL
Register All Albums Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

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).

RSS Feed

Your Options
Stop MySQL Displaying Error Messages

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'.



Related Resources

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


Search Engine Optimization by vBSEO 3.2.0 RC8