和數據庫直接接觸的類,只是封裝了增刪改查。
~~~
<?php
//判斷當前請求是否正常,如果不是通過index.php進行訪問的,就退出系統
if(!defined('ACCESS')) die('Hacking');
class Db{
public $conn;
public $host;
public $port;
public $user;
public $password;
public $db;
public $charset;
/*
* 構造函數,同時連接數據庫
* @param string 主機名
* @param string 端口號
* @param string 用戶名
* @param string 密碼
* @param string 數據庫
* @param string 編碼方式
*/
public function __construct($arr=array()){
//從配置文件中獲取必要信息
$this->host=isset($arr['host']) ? $arr['host'] : $GLOBALS['config']['mysql']['host'];
$this->port=isset($arr['port']) ? $arr['port'] : $GLOBALS['config']['mysql']['port'];
$this->user=isset($arr['user']) ? $arr['user'] : $GLOBALS['config']['mysql']['user'];
$this->password=isset($arr['password']) ? $arr['password'] : $GLOBALS['config']['mysql']['password'];
$this->db=isset($arr['db']) ? $arr['db'] : $GLOBALS['config']['mysql']['db'];
$this->charset=isset($arr['charset']) ? $arr['charset'] : $GLOBALS['config']['mysql']['charset'];
//連接數據庫
$this->connect_db();
}
/*
* 連接數據庫
*/
public function connect_db(){
$this->conn=mysql_connect($this->host.":".$this->port,$this->user,$this->password);
if (!$this->conn){
die("連接數據庫失敗:".mysql_error());
}
mysql_select_db($this->db);
mysql_query("set names {$this->charset}");
}
/*
* 操作dql查詢語句
*/
public function execute_dql($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_errno().":".mysql_error());
$i=0;
$arr=array();
while ($row=mysql_fetch_assoc($res)){ //將結果集存放在一個數組里面,這樣就用完結果集了
$arr[$i++]=$row;
}
mysql_free_result($res); //關閉結果集,釋放資源
return $arr;
}
/*
* 操作dml語句
*/
public function execute_dml($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_errno().":".mysql_error());
if (!$res) {
//失敗
return 0;
}else{
if (mysql_affected_rows($this->conn)>0){
//成功,并且修改了數據的數據
return 1;
}else {
//成功,但是沒有影響任何一行,語句正確不一定有真正的操作到了數據庫的數據
return 2;
}
}
}
//關閉連接
public function close_connect(){
if (!empty($this->conn)) { //先判斷一下,如果還連著,就斷掉
mysql_close($this->conn);
}
}
}
~~~
>[info]如果用戶連接數據庫時沒有自定義連接參數,就從配置文件中獲取
>[info]調用方法`execute_dql`后,將查詢得到的結果集放在到了二維數組中返回。
mysqli方式
~~~
<?php
//判斷當前請求是否正常,如果不是通過index.php進行訪問的,就退出系統
if(!defined('ACCESS')) die('Hacking');
class Db{
public $mysqli;
public $host;
public $port;
public $user;
public $password;
public $db;
public $charset;
/*
* 構造函數,同時連接數據庫
* @param string 主機名
* @param string 端口號
* @param string 用戶名
* @param string 密碼
* @param string 數據庫
* @param string 編碼方式
*/
public function __construct($arr=array()){
//從配置文件中獲取必要信息
$this->host=$GLOBALS['config']['mysql']['host'];
$this->port=$GLOBALS['config']['mysql']['port'];
$this->user=$GLOBALS['config']['mysql']['user'];
$this->password=$GLOBALS['config']['mysql']['password'];
$this->db=$GLOBALS['config']['mysql']['db'];
$this->charset=$GLOBALS['config']['mysql']['charset'];
//連接數據庫
$this->connect_db();
}
/*
* 連接數據庫
*/
public function connect_db(){
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
if ($this->mysqli->connect_error) {
die("連接數據庫失敗:".$mysqli->connect_error);
}
if (!$this->mysqli->set_charset("utf8")) {
die("設置默認字符編碼失敗:".$mysqli->error);
}
}
/*
* 操作mysqli預處理
*/
public function execute_prepare($sql){
$stmt = $this->mysqli->prepare($sql);
return $stmt;
}
/*
* 執行sql查詢語句
*/
public function execute_dql($stmt){
$stmt->execute();
$result = $stmt->get_result()->fetch_all();
//關閉結果集
$stmt->free_result();
$stmt->close();
//操作完成數據庫后,要及時關閉連接,不然比較浪費資源。還有如果不關閉,插入一條記錄時可能會變成插入兩條
$this->mysqli->close();
return $result;
}
/*
* 操作dml語句
*/
public function execute_dml($stmt){
$res=$stmt->execute();
if(!$res){
//失敗
$stmt->close();
$this->mysqli->close();
return 0;
}else{
if($stmt->affected_rows>0){
//成功,并且修改了數據的數據
$stmt->close();
$this->mysqli->close();
return 1;
}else{
//成功,但是沒有影響任何一行,語句正確不一定有真正的操作到了數據庫的數據
$stmt->close();
$this->mysqli->close();
return 2;
}
}
}
}
~~~