PHP persistent connection mysql_pconnect of function

  • 2020-05-12 02:20:50
  • OfStack

mysql_pconnect is especially useful for monitors that are executed by single 1 processes, especially 1 direct hold.

The usage of mysql_pconnect is similar to mysql_connect:
 
<?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: it is not a good choice to use mysql_pconnect for high-concurrency database requests. Consider caching and step by step.

The PHP persistent connection mysql_pconnect() function improves the efficiency ratio of JSP

Function usage:

 
$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 (), except for 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 return the connection identifier of this link without opening a new connection.

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

The mysql_pconnect () function can greatly improve the efficiency of MYSQL. However, this connection does not close automatically, which can cause some problems. Please be careful to close the unused connection immediately to avoid unnecessary errors.

In fact, I have written PHP's function mysql_pconnect() for persistent connection to the database before, but I didn't do any tests. Today, I did a small test, which is really good, especially when connecting to the remote database, the effect is very obvious.

Next, write 1 more PHP persistent connection database function application method (1 simple MYSQL class no simpler)

 
<?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");// A 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, I hope to help you, as for the test, I will not write, you test 1

Related articles: