


Introduction to the use of PHP persistent connection mysql_pconnect() function_PHP tutorial
mysql_pconnect is particularly suitable for a single process, especially a monitoring program that keeps executing.
Mysql_pconnect usage is similar to mysql_connect:
$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:
$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)
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.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment
