| 代碼如下 |
複製代碼 |
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.111cn.net/phper/php.html