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!

checking or connection possible without crashing

Status
Not open for further replies.

michelleqw

Programmer
Joined
Jan 4, 2004
Messages
120
Location
DE
Dear PHP users,

For our gastbook we need to connect to a MySQL database.
If there is a connection possible we want to show a button. If not we don't want to crash the page! The page have to show without the gastbook button.

Now we are using:
$db_link = mysql_connect("localhost", "root", "qwert") or die ("couldn't connect to MySQL\n");
When there is no connecting possible the page will stop!

Our question: is there any source to checkout the connection without crashing?

Nice regards,

Michelle.
 
Code:
<?php
$db=mysql_connect('localhost','','');

if(!$db){
// no database, say sorry or do nothing
}else{
// database OK, write a story, draw a button, have a party.
}

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hello KarveR,

Thanks for the information but the line

$db=mysql_connect('localhost','','');

will crash as the server is down or not excist!

Nice regards,

Michelle.
 
Code:
if (!($conn=mysql_connect($host, $username, $pwd)))  {
	  printf("error connecting to DB by user = $username and pwd=$pwd");
	  //do something else


}else{
          //show the form and buttons
}


Bastien

Cat, the other other white meat
 
I can think of two workarounds.

One workaround is to use PHP's Error handling and logging functions. You can use these functions to trap the error that would normally stop script execution.

The other workaround is to use PHP's "@" error-control operator to stop the error from being displayed. Your script can then check the return from mysql_connect() and react gracefully.

Of the two, I'd recommend using PHP's error-handling functions.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
As Sleipnir214 said, PHP's error handling is prolly the best way to go, however if you don't like reading and learning stuff, this is waaay easier but no-where near as professional ;-)

does exactly what you ask tho , sorry it was my bad for missing the @ earlier... work distarcts me at times .

Code:
<?php
if($db=@mysql_connect('localhost','root')){
echo "connected";
}else{
echo "not there so do nothing";
}
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top