If you insist on doing things this way it is better to make a custom query method which handles all this. Something like
function custom_mysql_query($query) {
$doDebug=true; // Set to true when developing and false when you are deploying for real.
$result=mysql_query($query);
if(!$result) {
if($doDebug) {
// We are debugging so show some nice error output
echo "Query failed\n<br><b>$query</b>\n";
echo mysql_error(); // (Is that not the name)
}
else {
// Might want an error message to the user here.
}
exit();
}
}
Then just call custom_mysql_query instead of mysql_query then you will always die if a query fails and if $debug
is true, you will also get the query which failed and the database error.
But really: You should NEVER use mysql_query or functions which call it(Such as the one I just wrote). It is far too unsafe to ever be used. (Far too difficult to avoid sql injections)
Use the pdo classes instead of the mysql_ methods(Google it, there are many tutorials and explanations online).