首页 > php与数据库 > php mysql_connect 与mysql_pconnect函数与实例教程

php mysql_connect 与mysql_pconnect函数与实例教程

当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。

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

/*

mysql_connect ,单个反问用户不会频繁的调用数据库教程,没必要保持连接,而且mysql的连接数也是有限制的, 使用 及时访问比较频繁,也最好使用mysql_connect,这样使用的过的资源可以立刻释放,否则,容易造成资源耗

*/

mysql_pconnect

/*

mysql_pconnect() 函数打开一个到 MySQL 服务器的持久连接

*/

<?php
$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() 建立的连接)。

*/


本文链接:http://www.phprm.com/database/ysql_pconnect-mysql_connec.html

收藏随意^^请保留教程地址.

标签:none

发表留言