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:
Copy codeThe Code is as follows:
<? Php
$ 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:
Copy codeThe 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)
Copy codeThe Code is as follows:
<? Php
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.