php mysql_connect 与mysql_pconnect函数与实例教程

作者:袖梨 2022-07-02
代码如下 复制代码

mysql_connect

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

/*

mysql_connect ,单个反问用户不会频繁的调用,没必要保持连接,而且mysql的连接数也是有限制的, 使用 及时访问比较频繁,也最好使用mysql_connect,这样使用的过的资源可以立刻释放,否则,容易造成资源耗
*/
mysql_pconnect
/*
mysql_pconnect() 函数打开一个到 MySQL 服务器的持久连接
*/

代码如下 复制代码
$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'; //标题

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 '数据库连接失败,请联系相关人员!';
exit;
}
}

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



/*
总结:
mysql_pconnect() 和 mysql_connect() 非常相似,但有两个主要区别:


其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)。
本站原创教程转载注明来源

相关文章

精彩推荐