PHP persistent connection mysql_pconnect () function usage introduction _ PHP Tutorial

Source: Internet
Author: User
PHP persistent connection mysql_pconnect () function usage introduction. Mysql_pconnect is especially applicable to a single process, especially monitoring programs that keep running. The usage of mysql_pconnect is similar to that of mysql_connect: The copy code is as follows :? Php $ connmy mysql_pconnect is especially applicable to a single process, especially a monitoring program that is always running.

The usage of mysql_pconnect is similar to that of mysql_connect:

The code is as follows:


$ Conn = mysql_pconnect ($ host, $ user, $ pwd );
Mysql_select_db ($ dbname, $ conn );
$ Result = mysql_query ("select * from table_name where col_id = 'Test _ id'", $ conn );
$ Result_detail = mysql_fetch_array ($ result );
$ Item = $ result_detail ['Col _ id'];
?>



Note: when processing database requests with high concurrency, using mysql_pconnect is not a good choice. you can consider caching and step-by-step processing.

PHP persistent connection to the mysql_pconnect () function can improve efficiency. JSP

Function usage:

The code is as follows:


$ DbHost = "localhost ";
$ DbUser = "root ";
$ DbPwd = "";
$ DbName = "zhoutang ";
$ StrSQL = "update tblUser set UserLC = UserLC + 1 where UserID = 100 ";
$ Link = mysql_connect ($ dbHost, $ dbUser, $ dbPwd) or die ('could not connect: '. mysql_error ());
Mysql_select_db ($ dbName );
Mysql_query ($ strSQL );
Mysql_close ($ link );



The usage is similar to mysql_connect (), but there are two differences:

First, mysql_pconnect () will first try to find a persistent connection that has been opened with the same user name and password on the same host. if it is found, the connection identifier of the link just returned without opening a new connection;

Second, after the function is executed, the SQL server connection will not be closed, and the connection will remain open for future use.

The mysql_pconnect () function can greatly improve MYSQL efficiency. However, this connection is not automatically closed and may cause some problems. Please close unused connections immediately, to avoid unnecessary errors.

In fact, I have previously written the PHP persistent connection database function mysql_pconnect (), but I did not perform any tests. today I did a small test, which is really good, especially when connecting to a remote database, the effect is even more obvious.

Next, let's write an application method for PHP persistent connection to the data inventory function (a simple MYSQL class)

The code is as follows:


Class mysql {
Private $ host;
Private $ user;
Private $ pw;
Private $ dbname;
Private $ code;
Public function _ construct ($ host, $ user, $ pw, $ dbname, $ code ){
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> pw = $ pw;
$ This-> dbname = $ dbname;
$ This-> code = $ code;
$ This-> conn ();
}
Public function conn (){
$ Conn = mysql_pconnect ($ this-> host, $ this-> user, $ this-> pw) or die ("links error"); // persistent connection
Mysql_select_db ($ this-> dbname, $ conn );
Mysql_query ("set names {$ this-> code }");
}
Public function query ($ SQL ){
$ Result = mysql_query ($ SQL );
Return $ result;
}
}
?>


The above method is helpful to everyone. as for testing, I will not write it. let's test it by yourself.

Bytes. The usage of mysql_pconnect is similar to that of mysql_connect: the code is as follows :? Php $ conn = my...

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.