class MySQL { private function __construct($server,$user,$pwd,$charset,$dbname){ $this->connect($server,$user,$pwd,$charset); $this->selectdb($dbname); } //实例化数据库对象 public static function getDB($server,$user,$pwd,$charset,$dbname){
$db = new MySQL($server,$user,$pwd,$charset,$dbname); return $db; }
//连接数据库标识 private $conn;
//连接服务器 public function connect($server,$user,$pwd,$charset){
http://blog.csdn.net/hopestar2/archive/2010/11/18/6019984.aspx
之前看过一些高手写的SQL类,那叫觉啊看的我直接很晕。目前还不敢用,因为还没研究透。
$this->selectdb($dbname);
} //实例化数据库对象
public static function getDB($server,$user,$pwd,$charset,$dbname){
$db = new MySQL($server,$user,$pwd,$charset,$dbname);
return $db;
}
//连接数据库标识
private $conn;
//连接服务器
public function connect($server,$user,$pwd,$charset){
$this->conn=mysql_connect($server,$user,$pwd);
mysql_query("SET character_set_connection=gb2312,character_set_results=$charset,character_set_client=binary",$this->conn);
if($this->conn==false){
echo "<center>连接服务器失败!<br />请刷新后重新连接!</center>";
die($this->conn);
}
}
//选择数据库
public function selectdb($dbname){
$con=mysql_select_db($dbname,$this->conn);
if($con==false){
echo "<center>选择数据库!<br />请刷新后重新连接!</center>";
die($con);
}
}
}