Optimize usability with MySQL built-in replication (vi) _php tutorial

Source: Internet
Author: User
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 = "";
$backup = "";
$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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.