<?PHP
class include_database const localhsot; //服務器名
const db; //數據庫名
const root; //用戶名
const root; //密碼
const gb2312; //編碼 private static $instance;
public $dsn;
public $dbuser;
public $dbpass;
public $sth;
public $dbh;
//初始化參數
public function __construct() $this->dsn = 'MySQL:host='.self::DB_HOST.';dbname='.self::DB_NAME;
$this->dbuser = self::DB_USER;
$this->dbpass = self::DB_PASS;
$this->connect();
$this->dbh->query('SET NAMES '.self::DB_CHARSET); } //連接數據庫
public function connect() try $this->dbh = new PDO($this->dsn,$this->dbuser,$this->dbpass); catch (PDOException $e) exit('連接失敗:'.$e->getMessage()); } //獲取數據表裡的字段
public function getFIElds($table) $this->sth = $this->dbh->query("DESCRIBE $table");
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $this->sth->fetchAll();
$this->sth = null;
return $result; private function getCode($table,$args)
$code = '';
if (is_array($args)) foreach ($args as $k => $v) if ($v == '') continue; $code .= "`$k`='$v',"; }
$code = substr($code,0,-1);
return $code; public function insert($table,$values) $sql = "INSERT INTO `$table` SET "; $set_sql = '';
if(is_array($values)) {
foreach($values as $k=>$v){
$set_sql .= "`$k` = '$v',"; $set_sql = substr($set_sql,0,-1); $sql .= $set_sql;
echo $sql;
if ($this->dbh->exec($sql)) $this->getPDOError();
return $this->dbh->lastInsertId(); return false;
//public function getCode($table,$args){ public function fetch($table,$condition = '',$sort = '',$limit = '',$field = '*',$debug = false) $sql = "SELECT {$fIEld} FROM `{$table}`";
if (false !== ($con = $this->getCondition($condition))) $sql .= $con; if ($sort != '') $sql .= " ORDER BY $sort"; if ($limit != ''){
$sql .= " LIMIT $limit"; //echo $sql;
//if ($debug)echo $sql;
$this->sth = $this->dbh->query($sql);
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $this->sth->fetchAll();
$this->sth = null;
return $result; public function fetchOne($table,$condition = null,$field = '*',$debug = false) $sql = "SELECT {$fIEld} FROM `{$table}`";
if (false !== ($con = $this->getCondition($condition))) $sql .= $con; // if ($debug)echo $sql;
$this->sth = $this->dbh->query($sql);
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $this->sth->fetch();
$this->sth = null;
return $result; public function getCondition($condition='') if ($condition != '') $con = ' WHERE';
if (is_array($condition)) $i = 0;
foreach ($condition as $k => $v) if ($i != 0){
$con .= " AND $k = '$v'";
}else {
$con .= " $k = '$v'"; $i++; }elseif (is_string($condition)) $con .= " $condition";
}else {
return false; return $con; return false; public function counts($table,$condition = '',$debug = false) $sql = "SELECT COUNT(*) AS num FROM `$table`";
if (false !== ($con = $this->getCondition($condition))) $sql .= $con; //if ($debug) echo $sql;
$count = $this->dbh->query($sql);
$this->getPDOError();
return $count->fetchColumn();
//按SQL語句查詢
public function DOSql($sql,$model='many',$debug = false) if ($debug){echo $sql;};
///echo $sql;
//if ($debug)echo $sql;
$this->sth = $this->dbh->query($sql);
$this->getPDOError();
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
if ($model == 'many') $result = $this->sth->fetchAll();
}else {
$result = $this->sth->fetch(); $this->sth = null;
return $result; public function update($table,$condition='')
$sql="UPDATE $table SET ";
$con=$condition;
// if (false !== ($con = $this->getCondition($condition))) $sql .= $con; //exit;
echo $sql;
//if ($debug)echo $sql;
//if ($debug)
//echo $sql;
if (($rows = $this->dbh->exec($sql)) > 0) $this->getPDOError();
// echo $rows;
return $rows; return false; public function increase($table,$condition,$field,$debug=false) $sql = "UPDATE `$table` SET $field = $fIEld + 1";
if (false !== ($con = $this->getCondition($condition))){
$sql .= $con; //if ($debug)echo $sql;
if (($rows = $this->dbh->exec($sql)) > 0){
$this->getPDOError();
return $rows; return false; public function del($table,$condition,$debug = false) $sql = "DELETE FROM $table";
if (false !== ($con = $this->getCondition($condition))) $sql .= $con;
}else {
exit('條件錯誤!'); //if ($debug)echo $sql;
echo $sql;
if (($rows = $this->dbh->exec($sql)) > 0) $this->getPDOError();
return $rows;
}else {
return false; }
public function query($sql,$method=''){
if($method == "U_Q" && function_exists('MySQL_unbuffered_query')){
$result = MySQL_unbuffered_query($sql,$this->_conn);
}else{
$result = MySQL_query($sql,$this->_conn);
}
$this->query_nums++;
if(!$result) $this->halt("error_sql",$sql);
return $result;
public function execute($sql) $this->dbh->exec($sql);
$this->getPDOError(); private function getPDOError() if ($this->dbh->errorCode() != '00000') $error = $this->dbh->errorInfo();
exit($error[2]); }
//關閉數據連接
public function __destruct() $this->dbh = null;
}
//*********************類方法使用
//$con=new include_database();
//$fields=$con->getFIElds("user");//根據表返回信息
//print_r($fIElds);
//echo $con->counts("teachers");//返回總共行數
//$row=$con->DOSql("select * from teachers");//根據sql語句查詢
//print_r($row);
//print_r($con->fetch("teachers"));//查詢信息
//$condition=$con->fetchOne('teachers',$condition = 'teacher_id=1',$fIEld = 'teacher_name',$debug = false);//根據條件查詢
//print_r($condition);
?>