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