Home > Backend Development > PHP Tutorial > Introduction to the use of PHP persistent connection mysql_pconnect() function_PHP tutorial

Introduction to the use of PHP persistent connection mysql_pconnect() function_PHP tutorial

WBOY
Release: 2016-07-21 15:20:52
Original
1225 people have browsed it

mysql_pconnect is particularly suitable for a single process, especially a monitoring program that keeps executing.

Mysql_pconnect usage is similar to mysql_connect:

Copy code 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: as When processing database requests with high concurrency, using mysql_pconnect is not a good choice. At this time, issues such as caching and step-by-step processing can be considered.

PHP persistent connection mysql_pconnect() function can improve efficiency and compete with JSP

Function usage:

Copy code 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);


Usage is similar to mysql_connect (), but there are two There are two differences:

First, mysql_pconnect() will first try to find a persistent connection that has been opened on the same host with the same username and password. If found, it will just return the connection identifier of this link instead of Open a new connection;

Second, when the function is executed, the connection to the SQL server will not be closed, this connection will remain open for future use.

The mysql_pconnect() function can greatly improve the efficiency of MYSQL. However, if this connection is not automatically closed, it will also cause some problems. Please be careful to close unused connections immediately to avoid unnecessary errors.

Actually, I have written the PHP function mysql_pconnect() for persistent connection to the database before, but I didn’t do any testing. I did a small test today and it turned out to be really good, especially when connecting to a remote database. , the effect is even more obvious.

Let’s write down the application method of PHP persistent connection data inventory function (a simple MYSQL class that cannot be simpler)

Copy code 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;
}
}
?>

I hope the above method will be helpful to everyone. As for the test, I won’t write it. You can test it yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325011.htmlTechArticlemysql_pconnect is especially suitable for a single process, especially a monitoring program that keeps executing. The usage of mysql_pconnect is similar to mysql_connect: Copy the code The code is as follows: ?php $conn = my...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template