Difference Between Connect and Pconnect in PHP

Introduction

In this article I discuss the two MySQL built-in connection functions "mysql_Connect()" and "mysql_Pconnect()" in PHP. Suppose you want to insert some data into the emp detail table of your database in a PHP application. The first thing that must be done is to create a connection for the table data for the operations and that is done using mysql_connect(), because before you can access data in a database, you must create a connection to the database and after performing all the operations on the database, you should close the connection using the mysql_close() function, but a Pconnection is already open and available for use at any time; the mysql_pconnect() function will not be closed and will persist for future use.

Syntax

Mysql_connect('localhost');

Example

<?php

//use for connection

{

$con= mysql_connect('localhost');

echo "$con";

}

?>

Output

Mysql pconnect function in php.jpg

Syntax

Mysql_pconnect('localhost');

 

The Pconnect function uses minimal resources because the mysql_pconnect() function is already connected with your database connection; it does need to have a connection established with the database every time the page is loaded, for when you want to persistent a connection.
When you want a connection then you first find a persistent connection link that is already open with the same host, username and password. The persistent connection is made with a max connection and connected with max clients. This connection is a multi-credential connection.

Example

<?php

//use for persistence connection

$conn = mysql_pconnect('localhost');

echo $conn;

?>

Output

Mysql pconnect function in php.jpg


Similar Articles