Difference between mysql_connect() and mysql_pconnect() Functions in PHP
mysql_connect() Function: The mysql_connect() function is used to establish a new connection with the database. This connection is established when the script starts its execution. After establishing this connection with the database, it will be valid or be connected with the database only until the script is executed. It means that as soon as the script stops its execution this connection with the database will also close. The mysql_close() method is used to close the connection with the database.
Example 1: In the following code, if the connection becomes successful it will display the echo part or in case of any error it will display the die part.
Database Connection Established Successfully.
Example 2: In the following code, we are connecting to geeksforgeeks.org database on port 3307.
Connected successfully to GFG Database
mysql_pconnect() Function: The mysql_pconnect() is a kind of function which is slightly different from that of mysql_connect(). When you use this function to connect to a database then it will search whether there is any other existing connection being made to the database using the same username and password, If this condition appears to be true then it returns the resource ID. It will not make the connection again and again whenever the script will be called and it will also not end the connection when the script stops execution. This type of connection is called a persistent connection.
Example 1: In the following code, we are establishing a persistent connection by using mysql_pconnect() function.
Persistent Connection Established
Example 2: In the following code, we are connecting to geeksforgeeks.org database on the port 3307 by using Persistent Connection (mysql_pconnect()).
Connected successfully to GFG Database using Persistent Connection
Difference between mysql_connect() and mysql_pconnect() functions: mysql_connect() function mysql_pconnect() function
This function establishes a connection with the database when the script is called. This function first checks whether a connection with the same username and password is created or not and if not then it establishes a connection. The mysql_close() method is used to close the connection with the database. The mysql_close() does not close the connection with the database. It is a time-consuming function as it establishes a connection every time it is being called. It is a time-saving function as it does not establish a connection every time it is called rather connection establishment is performed only once. It is used when a new connection is to be established. It is used when we don’t want to disconnect from the database and keep it for future use. It requires more memory as the connection is established every time this function is called. It requires less memory as the connection is established only once Due to its complex use, it is not much user-friendly Due to its simplicity, it is more user-friendly. The database is opened every time the page is loaded using myql_connect() method. The database is not opened every time the page is loaded with the mysql_pconnect() method.