Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some mysql_connect help!! Pls Help 1

Status
Not open for further replies.

Forri

Programmer
Joined
Oct 29, 2003
Messages
479
Location
MT
Hi

I'm trying to connect to the database, which currently works but would like to set a default error page instead of using the die function.

i was trying something like this:

Code:
if (!$connection = mysql_connect($DB_Host, $DB_User, $DB_Pass))
{
  SET_SYSTEM_ERROR('Could not connect to database', mysql_error()); //Sets error variables
  SYSTEM_ERROR(); //Shows the html
  exit;
}


But the above problem is that the MySQL still shows!

How can i fix this?

ps. another question:
if i would like to set some default values how best would i store them : in a SESSION variable; in an array of a SESSION variable or global variables?


Thanks
Nick
 
Why not just use a simply if statement. First, try to connect to the database. Then, set an if statement that says something if you have not connected to the database, like this:

Code:
if (!$connection = mysql_connect($DB_Host, $DB_User, $DB_Pass))
{
$error_msg = "Could not connect to database".mysql_error();
print $error_msg;
exit;
} 

I'm not sure about that session thing, but I hope this helps.

Peace out,
Peace Co.
 
Error handling is very important and a graful failure is a good idea. Here's how you can suppress the MySQL message - only recommended if you handle the error in the code following the connection statement:
Code:
$link = @mysql_connect($host,$user,$pass);
if (!$link) {
   // your error code when link fails
}
The "@" operator should only be used if you handle the error otherwise. Alternatively you could change the level of error reporting in your php.ini which also should be only done if the errors are handeled explicitly in your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top