php mysql_connect 与mysql_pconnect函数与实例教程
当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的持久连接,如果找到,则返回此连接标识而不打开新连接,代码如下:
mysql_connect
mysql_connect($this->root,$this->user,$this->pass)
mysql_connect,单个反问用户不会频繁的调用数据库,没必要保持连接,而且mysql的连接数也是有限制的,使用及时访问比较频繁,也最好使用mysql_connect,这样使用的过的资源可以立刻释放,否则,容易造成资源耗,php实例代码如下:
<?php
/*
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; //开源代码phprm.com
}
}
function close() {
mysql_close($this->links);
}
}总结:
mysql_pconnect() 和mysql_connect() 非常相似, 但有两个主要区别:
当脚本执行完毕后到SQL服务器的连接不会被关闭, 此连接将保持打开以备以后使用, mysql_close() 不会关闭由mysql_pconnect() 建立的连接 .
本文地址:http://www.phprm.com/develop/fs5446.html
转载随意,但请附上文章地址:-)