PHP Persistent Connection mysql_pconnect () function use introduction _php tips

Source: Internet
Author: User
Mysql_pconnect is particularly applicable in a single process, particularly the monitoring procedures that have been maintained.

The Mysql_pconnect usage is similar to the mysql_connect:
Copy Code code 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: Using Mysql_pconnect is not a good choice as a high concurrent processing database request, so you can consider caching and step-by-step issues.

PHP Persistent Connection mysql_pconnect () function to increase efficiency competition JSP

Function usage:

Copy Code code 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 only two differences:

First, Mysql_pconnect () first tries to find a persistent connection that has been opened with the same username and password on the same host, and if found, just returns the connection identifier for the link without opening the new connection;

Second, when the function executes, the SQL Server's connection is not closed and the connection remains open for later use.

Mysql_pconnect () function can greatly improve the efficiency of MySQL, however, this connection does not automatically shut down, but also caused some problems, please note that the unused connection immediately shut down to avoid unnecessary errors occur.

In fact, I have written PHP persistent connection database function Mysql_pconnect (), but did not do any testing, today did a small test, really good, especially when the remote database connection, the effect is very obvious.

And then write the application method of the PHP persistent connection data inventory function (a simple MySQL class can not be simply)

Copy Code code 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 methods, I hope to help you, as for the test, I will not write, we test it yourself

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.