


Detailed explanation of the difference between mysql_connect and mysql_pconncet in PHP_PHP tutorial
Let’s talk about the difference between mysql_connect and mysql_pconnect . The usage of these two functions is similar. Some people on the Internet say that pconnect should be used. Ambiguous. So what is this thing like?
A permanent link does not mean that the server opens a connection and then everyone shares the link. Permanent connections also open a connection for each client. If 200 people visit, there will be 200 connections. In fact, mysql_pconnect() itself does not do much processing. The only thing it does is not to actively close the mysql connection after PHP is running.
When PHP is run in cgi mode, pconnect and connect are basically absent. The difference is that in the cgi method, each PHP access starts a process. After the access is completed, the process ends and all resources are released. When PHP is run in the apache module mode, since apache uses a process pool, an httpd process ends. After that, it will be put back into the process pool, which means that the mysql connection resource opened with pconnect will not be released, so it can be reused when there is the next connection request. This makes it possible to use Apache when the concurrent access volume is not large. , due to the use of pconnect, PHP saves the time of repeatedly connecting to the db, making the access speed faster. This should be easier to understand. However, when the concurrent access volume of apache is large, if pconnect is used, it will be occupied by some previous httpd processes. If the mysql connection is not closed, it may be that mysql has reached the maximum number of connections, so that some subsequent requests will never be satisfied. If the maximum number of mysql connections is set to 500, and the maximum number of simultaneous accesses of apache is set to 2000, assuming that all Access will require access to the db, and the operation time will be relatively long. When the current 500 httpd requests are not completed, subsequent httd processes will not be able to connect to mysql (because the maximum number of mysql connections has been reached). Only the current 500 Mysql can be connected only after the httpd process ends or is reused.
When the db operation is complex and takes a long time, httpd will fork many concurrent processes, and the httpd process generated first will not release the db connection. , making the later httpd process unable to connect to the db. Because the mysql connections of other httpd processes are not reused, many connection timeouts will occur. When the number of concurrent visits is not high, using pconnect can simply improve the access speed, but after the amount of concurrency increases, whether to use pconnect again depends on the programmer's choice.
In my personal opinion, PHP now The connection to mysql does not really use the connection pool, pconnect is just equivalent to borrowing the process pool of apache, so pconnect cannot improve the efficiency of accessing the db when the amount of concurrent access is large.
In actual applications, using mysql_pconnect, each refresh and requesting a new page is faster, while using mysql_connect, each refresh must be re-requested. When the database connection is slow, the difference can be seen. . When your database connection is slow, the DB operation is not very complicated, and your program is confident enough that it will not cause deadlock, or you have control over the server and meet any two of the above four conditions , then you can use pconnect.
pconnect does not need to be closed in the script. You can set the lifetime in mysql, or you can write a shell to scan regularly and kill connections that have been dormant for too long. One sentence summary: To make good use of pconnect, it is not only a matter of php scripts, but also related to the settings of the database and server.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
