Examples of MySQL error management and connection failure options.
Error handling with user friendly messages:
$dbconnect = mysql_connect ("database_servername", "database_username", "password"); if (!$dbconnect) { exit ("Unable to establish a connection - please try later."); }
mysql_select_db("name_of_the_database_to_work_with") or die ("Unable to select the database at this time - please try later");
/*************************************************************************** Querying a database: ****************************************************************************/
$query="select name, id, address from table_name"; $result=mysql_query($query)or die ("The function requested is temporarily unavailable");
/*************************************************************************** Querying a database, and calling a generic error file should the query fail: ****************************************************************************/
$query="select name, id, address from table_name"; $result=mysql_query($query)or die (include'error.php');
/*************************************************************************** Recommended method -- should the following connection attempt fail, a user friendly page is presented, and an e-mail alert automatically sent to the sites admin: ****************************************************************************/
$dbconnect = mysql_connect ("database_servername", "database_username", "password"); if (!$dbconnect)
{
mail("admin@yoursite.com", "Message from MySQL", "Connection error!");