Home >Backend Development >PHP Tutorial >php mysql_connect and mysql_pconnect functions and examples tutorial_PHP tutorial

php mysql_connect and mysql_pconnect functions and examples tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:04:27861browse

When connecting, this function 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 return the connection ID without opening a new connection. Second, the connection to the SQL server is not closed when the script completes, the connection remains open for future use (mysql_close() does not close the connection established by mysql_pconnect()).

php tutorial mysql tutorial_connect and mysql_pconnect function and example tutorial

mysql_connect

mysql_connect($this->root,$this->user,$this->pass)

/*

mysql_connect, a single rhetorical user will not frequently call the database tutorial, there is no need to maintain the connection, and the number of mysql connections is also limited. If you use timely access more frequently, it is best to use mysql_connect, so that the used resources can be Release immediately, otherwise it will easily cause resource consumption
*/
mysql_pconnect
/*
The mysql_pconnect() function opens a persistent connection to the MySQL server
*/

$con = mysql_pconnect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}


class testLinkMysql{
public $conn;
public $root='localhost';
public $user='root';//'loupan';
public $pass='root';//'loupandsffds';
public $db='dbname';
public $charset='gbk';
public $links='c'; //Title

function __construct() {
If( !$this->conn )
{
$this->connect();
}  
}

 
function __destruct() {
if( $this->conn )
{
                   $this->close();
}
}


function MysqlConnect()
{
Try{
If( 'p' == $this->links )
{
            $this->conn = mysql_pconnect($this->root,$this->user,$this->pass) or die(mysql_error());                                                                 }
else
{
$this->conn = mysql_connect($this->root,$this->user,$this->pass) or die( mysql_error());
}
Mysql_select_db($this->db,$this->conn);
Mysql_query("set Names '$this->charset'");
}catch (Exception $e){
echo 'Database connection failed, please contact relevant personnel!';
exit;
}  
}

function close()
{
Mysql_close($this->links);
}
}


/*
Summary:

mysql_pconnect() and mysql_connect() are very similar, but have two main differences:

When connecting, this function 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 return the connection ID without opening a new connection.

Second, the connection to the SQL server is not closed when the script completes, the connection remains open for future use (mysql_close() does not close the connection established by mysql_pconnect()).
Please indicate the source when reprinting original tutorials on this site http://www.bKjia.c0m/phper/php.html

http://www.bkjia.com/PHPjc/630873.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630873.htmlTechArticleWhen connecting, this function will first try to find an open file on the same host with the same username and password. (persistent) connection, if found, returns this connection ID without...
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