Mysqlhasgoneaway caused by PHP connection to the database for a long time

Source: Internet
Author: User
Mysqlhasgoneaway public function connect (){
If ($ this-> conn = "pconn "){
// Permanent link
$ This-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
} Else {
// Even if the link
$ This-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
}

If (! Mysql_select_db ($ this-> db_database, $ this-> conn )){
If ($ this-> show_error ){
$ This-> show_error ("database unavailable:", $ this-> db_database );
}
}
Mysql_query ("set names $ this-> coding ");
}

The above is the writing of database connections. how can I avoid errors? Are there other methods?


Reply to discussion (solution)

Ask the experts to give a method

The database connection times out or exceeds the non-operation timeout setting and is forcibly disconnected from the database. Another possibility is that the database is unstable. Persistent connections are not recommended ~~ We do not recommend that you obtain super-large data results at a time ~

function dbConnect($hostname,$username,$pass,$db_name,$pconnect = 0) {    $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';        if(!$connect) {        $connect = @$func($hostname,$username,$pass) or die("Mysql_Error : ".mysql_error()."
Mysql Error Num : ".mysql_errno().""); } @mysql_select_db($db_name, $connect) or die(" Mysql_Error : ".mysql_error()."
Mysql Error Num : ".mysql_errno().""); return $connect;}

We recommend that you close the connection to the database after the query is completed.

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.