What is the difference between mysql_pconnect and mysql_connect? MySQL database php
There are two main differences in the manual
1. do not send connection requests repeatedly
2. mysql_close does not work
The first point is a bit hard to understand. Isn't mysql_connect the same as mysql _ pconnect if I don't send a request again or in the middle of mysql_close.
If so
Then I define a function.
Function connect (){
Static $ connect_id;
If (! $ Connect_id)
$ Connect_id = mysql_connect ("localhost", "hao", "hao ");
Return $ connect_id;
}
Call this function to connect to the database. Connect ()
There will be no repeated connection requests.
Is it the same as mysql _ pconnect?
Thank you!
Reply to discussion (solution)
Resource mysql_connect ([string $ server [, string $ username [, string $ password [, bool $ new_link [, int $ client_flags])
When you do not specify the true value for $ new_link, their behavior is the same. The second call of the same parameter will not create a new connection, but will return the opened Connection ID
Isn't mysql_connect the same as mysql _ pconnect if I don't send a request again or in the middle of mysql_close.
No!
Database connection is divided into two phases
Phase 1: establish a physical connection with the database server
Stage 2: log on to the database server and establish a business connection with the database
For mysql_connect, mysql_close will disconnect the two connections (mysql_close will be automatically called after the php program ends)
For mysql_pconnect, mysql_close does not work. After the php program ends, the service link to the database is automatically cut off. Physical connections are retained to be reused.
Isn't mysql_connect the same as mysql _ pconnect if I don't send a request again or in the middle of mysql_close.
No!
Database connection is divided into two phases
Phase 1: establish a physical connection with the database server
Stage 2: log on to the database server and establish a business connection with the database
For mysql_connect, mysql_close will disconnect the two connections (mysql_close will be automatically called after the php program ends)
For mysql_pconnect, mysql_close does not work. After the php program ends, the service link to the database is automatically cut off. Physical connections are retained to be reused.
Thank you for your answer. I still don't quite understand the reuse of mysql_pconnect. Can cross-script reuse be avoided. For example, MySQL _ pconnect is used for script A, and script B is enabled when script A is not executed. Then Script B can use the connection of script A, instead of connecting to the database again.
What are the advantages of mysql_pconnect? I have seen some online database operation classes. this is useless.
I don't know.
Database connection is always necessary
Generally, the network connection is slow. that is to say, if the physical connection to the database is not cut off, the next access may be faster (provided that the network channel during the last connection can be reused)
Persistent connections are generally used when web services and database services are not on the same machine.
As you described, the connection cannot be reused because the connection is idle before script A is executed.
Database connection is always necessary
Generally, the network connection is slow. that is to say, if the physical connection to the database is not cut off, the next access may be faster (provided that the network channel during the last connection can be reused)
Persistent connections are generally used when web services and database services are not on the same machine.
As you described, the connection cannot be reused because the connection is idle before script A is executed.
Thank you for understanding the moderator.
I want to explain why
Persistent connections are generally used when web services and database services are not on the same machine.
As mentioned above, the network is used instead of the same machine.
However, the network is not intended for you, so the actual connection is relatively slow. Therefore, it is more cost-effective to keep the physical connection.
In the same machine and on, it is faster because it does not need to pass through the network. On the contrary, persistent connections occupy a large amount of resources because the database cannot be connected because it is too late to be idle.