search
HomeBackend DevelopmentPHP TutorialIntroduction 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:

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...
Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

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

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

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.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

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

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

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.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

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.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

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.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

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

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment