php mysql_connect 與mysql_pconnect函數與執行個體教程_PHP教程

來源:互聯網
上載者:User
當串連的時候本函數將先嘗試尋找一個在同一個主機上用同樣的使用者名稱和密碼已經開啟的(持久)串連,如果找到,則返回此串連標識而不開啟新串連。 其次,當指令碼執行完畢後到 SQL 伺服器的串連不會被關閉,此串連將保持開啟以備以後使用(mysql_close() 不會關閉由 mysql_pconnect() 建立的串連)。

php教程 mysql教程_connect 與mysql_pconnect函數與執行個體教程

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() 建立的串連)。
本站原創教程轉載註明來源http://www.bKjia.c0m/phper/php.html

http://www.bkjia.com/PHPjc/630873.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630873.htmlTechArticle當串連的時候本函數將先嘗試尋找一個在同一個主機上用同樣的使用者名稱和密碼已經開啟的(持久)串連,如果找到,則返回此串連標識而不...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.