Fifth step: An improved Database connection program
$#@60;? Php
/********************************************************
function Db_connect_plus ()
Returns a link identifier on success, or false On error
********************************************************/
function Db_connect_plus () {
$username = "username";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
$timeout = 15; Timeout in seconds
if ($fp = Fsockopen ($primary, 3306, & $errno, & $errstr, $timeout)) {
Fclose ($FP);
return $link = mysql_connect ($primary, $username, $password);
}
if ($fp = Fsockopen ($secondary, 3306, & $errno, & $errstr, $timeout)) {
Fclose ($FP);
return $link = mysql_connect ($secondary, $username, $password);
}
return 0;
}
? $#@62;
This newly improved function provides us with an adjustable timeout feature that is missing from the mysql_connect function. If the connection fails immediately, such as the Machine "alive", but Mysqld "when" dropped, the function immediately moved to the second server. The above function is quite robust, test it before attempting to connect, check to see if the service is listening on the specified port, and let your script time out after an acceptable period of time, allowing you to properly handle the error situation. If you modified the default port 3306, make sure to modify the port number.
http://www.bkjia.com/PHPjc/532384.html www.bkjia.com true http://www.bkjia.com/PHPjc/532384.html techarticle The fifth step: An improved database connection program $#@60;? PHP/******************************************************** function Db_connect_plus () returns a link identifier on Succes ...