<?php
/**
其他未實(shí)現(xiàn)的
1、綁定列到php變量請(qǐng)使用? $db->statement->bindColumn(1,$name);
*/
class db {
? ? private $pdo = null;
? ? public $statement = null;
? ? private $is_addsla = false;
? ? public $options = array(
? ? ? ? \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ",
? ? );
? ? public function __construct($host,$user="root",$pass="",$dbname="",$no_persistent=false){
? ? ? ? $this->options[\PDO::MYSQL_ATTR_INIT_COMMAND] .= "utf8";
? ? ? ? if($no_persistent){
? ? ? ? ? ? $this->options[\PDO::ATTR_PERSISTENT] = false;
? ? ? ? }
? ? ? ? $dsn = "mysql:host={$host};dbname={$dbname}";
? ? ? ? try {
? ? ? ? ? ? $this->pdo = new \PDO($dsn,$user,$pass,$this->options);
? ? ? ? ? ? $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
? ? ? ? ? ? /* 下面2行為避免查出的int字段自動(dòng)變string */
? ? ? ? ? ? $this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
? ? ? ? ? ? $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
? ? ? ? } catch (PDOException $e) {
? ? ? ? ? ? print "Error!: " . $e->getMessage() . "<br>";
? ? ? ? ? ? die();
? ? ? ? }
? ? }
? ? /**
? ? 全局屬性設(shè)置,包括:列名格式和錯(cuò)誤提示類型? ? 可以使用數(shù)字也能直接使用參數(shù)
? ? */
? ? public function setAttr($param,$val=''){
? ? ? ? if(is_array($param)){
? ? ? ? ? ? foreach($param as $key=>$val){
? ? ? ? ? ? ? ? $this->pdo->setAttribute($key,$val);
? ? ? ? ? ? }
? ? ? ? }else{
? ? ? ? ? ? if($val!=''){
? ? ? ? ? ? ? ? $this->pdo->setAttribute($param,$val);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? 生成一個(gè)編譯好的sql語句模版 你可以使用 ? :name 的形式
? ? 返回一個(gè)statement對(duì)象
? ? */
? ? public function doPrepare($sql=""){
? ? ? ? // addLog(array('file_name'=>'sqlPrepare','content'=>$sql));
? ? ? ? if($sql==""){
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? try{
? ? ? ? ? ? $this->statement = $this->pdo->prepare($sql);
? ? ? ? ? ? return $this->statement;
? ? ? ? }catch(PDOException $e){
? ? ? ? ? ? $exception = $e->getMessage();
? ? ? ? ? ? $array = array('exception' => $exception);
? ? ? ? ? ? $this->showSqlError($array);
? ? ? ? }
? ? }
? ? /**
? ? 執(zhí)行Sql語句,一般用于 增、刪、更新或者設(shè)置? 返回影響的行數(shù)
? ? */
? ? public function exec($sql){
? ? ? ? if($sql==""){
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? try{
? ? ? ? ? ? return $this->pdo->exec($sql);
? ? ? ? }catch(Exception $e){
? ? ? ? ? ? return $e->getMessage();
? ? ? ? }
? ? }
? ? /**
? ? 執(zhí)行有返回值的查詢,返回PDOStatement? 可以通過鏈?zhǔn)讲僮?,可以通過這個(gè)類封裝的操作獲取數(shù)據(jù)
? ? */
? ? public function query($sql){
? ? ? ? if($sql=""){
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? try{
? ? ? ? ? ? $this->statement = $this->pdo->query($sql);
? ? ? ? ? ? return $this->statement;
? ? ? ? }catch(Exception $e){
? ? ? ? ? ? return $e->getMessage();
? ? ? ? }
? ? }
? ? /**
? ? 開啟事務(wù)
? ? */
? ? public function beginTA(){
? ? ? ? return $this->pdo->beginTransaction();
? ? }
? ? /**
? ? 提交事務(wù)
? ? */
? ? public function commit(){
? ? ? ? return $this->pdo->commit();
? ? }
? ? /**
? ? 事務(wù)回滾
? ? */
? ? public function rollBack(){
? ? ? ? return $this->pdo->rollBack();
? ? }
? ? public function lastInertId(){
? ? ? ? return $db->lastInsertId();
? ? }
? ? //**? PDOStatement 類操作封裝? ? **//
? ? /**
? ? 讓模版執(zhí)行SQL語句,1、執(zhí)行編譯好的 2、在執(zhí)行時(shí)編譯
? ? */
? ? public function execute($param=""){
? ? ? ? if(is_array($param)){
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? return $this->statement->execute($param);
? ? ? ? ? ? }catch (Exception $e){
? ? ? ? ? ? ? ? //return $this->errorInfo();
? ? ? ? ? ? ? ? return $e->getMessage();
? ? ? ? ? ? }
? ? ? ? }else{
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? return $this->statement->execute();
? ? ? ? ? ? }catch(Exception $e){
? ? ? ? ? ? ? ? /* 返回的錯(cuò)誤信息格式
? ? ? ? ? ? ? ? [0] => 42S22
? ? ? ? ? ? ? ? [1] => 1054
? ? ? ? ? ? ? ? [2] => Unknown column 'col' in 'field list'
? ? ? ? ? ? ? ? return $this->errorInfo();
? ? ? ? ? ? ? ? */
? ? ? ? ? ? ? ? return $e->getMessage();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? 參數(shù)1說明:
? ? PDO::FETCH_BOTH? ? 也是默認(rèn)的,兩者都有(索引,關(guān)聯(lián))
? ? PDO::FETCH_ASSOC? ? 關(guān)聯(lián)數(shù)組
? ? PDO::FETCH_NUM? ? ? 索引
? ? PDO::FETCH_OBJ? ? ? ? ? 對(duì)象
? ? PDO::FETCH_LAZY? ? 對(duì)象 會(huì)附帶queryString查詢SQL語句
? ? PDO::FETCH_BOUND? ? 如果設(shè)置了bindColumn,則使用該參數(shù)
? ? */
? ? public function fetch($fetch_style=db::FETCH_BOTH){
? ? ? ? if(is_object($this->statement)){
? ? ? ? ? ? return $this->statement->fetch($fetch_style);
? ? ? ? }else{
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
? ? /**
? ? 參數(shù)1說明:
? ? PDO::FETCH_BOTH? ? 也是默認(rèn)的,兩者都有(索引,關(guān)聯(lián))
? ? PDO::FETCH_ASSOC? ? 關(guān)聯(lián)數(shù)組
? ? PDO::FETCH_NUM? ? ? 索引
? ? PDO::FETCH_OBJ? ? ? ? ? 對(duì)象
? ? PDO::FETCH_COLUMN? 指定列 參數(shù)2可以指定要獲取的列
? ? PDO::FETCH_CLASS? ? ? ? 指定自己定義的類
? ? PDO::FETCH_FUNC? ? 自定義類 處理返回的數(shù)據(jù)
? ? PDO_FETCH_BOUND 如果你需要設(shè)置bindColumn,則使用該參數(shù)
? ? 參數(shù)2說明:
? ? 給定要處理這個(gè)結(jié)果的類或函數(shù)
? ? */
? ? public function fetchAll($fetch_style=db::FETCH_BOTH, $handle=''){
? ? ? ? if($handle!=''){
? ? ? ? ? ? return $this->statement->fetchAll($fetch_style,$handle);
? ? ? ? }else{
? ? ? ? ? ? return $this->statement->fetchAll($fetch_style);
? ? ? ? }
? ? }
? ? /**
? ? 以對(duì)象形式返回 結(jié)果 跟fetch(PDO::FETCH_OBJ)一樣
? ? */
? ? public function fetchObject($class_name){
? ? ? ? if($clss_name!=''){
? ? ? ? ? ? return $this->statement->fetchObject($class_name);
? ? ? ? }else{
? ? ? ? ? ? return $this->statement->fetchObject();
? ? ? ? }
? ? }
? ? /**
? ? public function bindColumn($array=array(),$type=EXTR_OVERWRITE){
? ? if(count($array)>0){
? ? extract($array,$type);
? ? }
? ? //$this->statement->bindColumn()
? ? }
? ? */
? ? /**
? ? 以引用的方式綁定變量到占位符(可以只執(zhí)行一次prepare,執(zhí)行多次bindParam達(dá)到重復(fù)使用的效果)
? ? */
? ? public function bindParam($parameter, $variable, $data_type=db::PARAM_STR, $length=6){
? ? ? ? return $this->statement->bindParam($parameter,$variable,$data_type,$length);
? ? }
? ? /**
? ? 返回statement記錄集的行數(shù)
? ? */
? ? public function rowCount(){
? ? ? ? return $this->statement->rowCount();
? ? }
? ? public function count(){
? ? ? ? return $this->statement->rowCount();
? ? }
? ? /**
? ? 關(guān)閉編譯的模版
? ? */
? ? public function close(){
? ? ? ? return $this->statement->closeCursor();
? ? }
? ? public function closeCursor(){
? ? ? ? return $this->statement->closeCursor();
? ? }
? ? /**
? ? 返回錯(cuò)誤信息也包括錯(cuò)誤號(hào)
? ? */
? ? private function errorInfo(){
? ? ? ? return $this->statement->errorInfo();
? ? }
? ? /**
? ? 返回錯(cuò)誤號(hào)
? ? */
? ? private function errorCode(){
? ? ? ? return $this->statement->errorCode();
? ? }
? ? //簡化操作--------------------------------------------------
? ? /*
? ? $select:要獲取的屬性
? ? $table:數(shù)據(jù)表名稱
? ? $condition:獲取條件
? ? $order:數(shù)據(jù)排序
? ? $limit:獲取幾條數(shù)據(jù)
? ? */
? ? public function select($array) {
? ? ? ? $array = array_merge(array('field'=>'*','table'=>'','where'=>'','group'=>'','order'=>'','limit'=>''),$array);
? ? ? ? if(!$array['table']){
? ? ? ? ? ? $array['table'] = $this->getTable();
? ? ? ? }
? ? ? ? if( is_array($array['order']) && isset($array['order']['order']) && ( $array['order']['order'] == 'desc' || $array['order']['order'] == 'asc' ) && isset($array['order']['field']) ) {
? ? ? ? ? ? $array['order'] = "order by ".$array['order']['field']. $array['order']['order'];
? ? ? ? } elseif( $array['order'] && is_string($array['order']) ) {
? ? ? ? ? ? $array['order'] = " order by {$array['order']}";
? ? ? ? } else $array['order'] = '';
? ? ? ? if($array['group']) {
? ? ? ? ? ? $array['group'] = " GROUP BY ".$array['group'];
? ? ? ? }
? ? ? ? if($array['limit']) {
? ? ? ? ? ? $array['limit'] = " LIMIT ".$array['limit'];
? ? ? ? }
? ? ? ? $this->sql =
? ? ? ? ? ? "SELECT ".? $array['field'].
? ? ? ? ? ? " FROM ".? ? $array['table'].
? ? ? ? ? ? " WHERE ". $array['where'].
? ? ? ? ? ? $array['group'].
? ? ? ? ? ? $array['order'].
? ? ? ? ? ? $array['limit'];
? ? ? ? // addLog(array('file_name'=>'sqlSelect','content'=>$this->sql));
? ? ? ? if($this->result = $this->doPrepare($this->sql)) {
? ? ? ? ? ? return $this;
? ? ? ? }
? ? ? ? return $this;
? ? }
? ? //獲取所有查詢到的數(shù)據(jù)$select,$table,$condition = '',$order = array(), $limit = ''
? ? public function getAll($array) {
? ? ? ? $this->select($array);
? ? ? ? $arrData = array();
? ? ? ? if( $this->statement ) {
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? $this->result = $this->statement->execute();
? ? ? ? ? ? ? ? $arrData = $this->statement->fetchAll(\PDO::FETCH_ASSOC);
? ? ? ? ? ? }catch(\PDOException $e){
? ? ? ? ? ? ? ? $this->showSqlError(array('exception'=>$e->getMessage()));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $arrData;
? ? }
? ? //獲取一條查詢到的數(shù)據(jù)
? ? public function getOne($array) {
? ? ? ? $array['limit'] = '0,1';
? ? ? ? $this->select($array);
? ? ? ? $arrData = array();
? ? ? ? if( $this->statement ) {
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? $this->statement->execute();
? ? ? ? ? ? ? ? $arrData = $this->statement->fetch(\PDO::FETCH_ASSOC);
? ? ? ? ? ? }catch(\PDOException $e){
? ? ? ? ? ? ? ? $this->showSqlError(array('exception'=>$e->getMessage()));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $arrData;
? ? }
? ? public function add($array){
? ? ? ? $array = array_merge(array('table'=>''),$array);
? ? ? ? if(!is_array($array['data'])){
? ? ? ? ? ? die('data必須是數(shù)組!');
? ? ? ? }
? ? ? ? if(!$array['table']){
? ? ? ? ? ? $array['table'] = $this->getTable();
? ? ? ? }
? ? ? ? $cols = array();
? ? ? ? $vals = array();
? ? ? ? foreach($array['data'] as $key=>$val){
? ? ? ? ? ? $cols[]=$key;
? ? ? ? ? ? $vals[]="'".$this->addsla($val)."'";
? ? ? ? }
? ? ? ? $sql? = "INSERT INTO {$array['table']} (";
? ? ? ? $sql .= implode(",",$cols).") VALUES (";
? ? ? ? $sql .= implode(",",$vals).")";
? ? ? ? // addLog(array('file_name'=>'sqlInsert','content'=>$sql));
? ? ? ? return $this->exec($sql);
? ? }
? ? public function update($array){
? ? ? ? $array = array_merge(array('table'=>''),$array);
? ? ? ? if(!is_array($array['data'])){
? ? ? ? ? ? die('data必須是數(shù)組!');
? ? ? ? }
? ? ? ? $set = array();
? ? ? ? foreach($array['data'] as $key=>$val){
? ? ? ? ? ? $set[] = $key."='".trim($this->addsla($val))."'";
? ? ? ? }
? ? ? ? if(!$array['table']){
? ? ? ? ? ? $array['table'] = $this->getTable();
? ? ? ? }
? ? ? ? $sql = "UPDATE {$array['table']} SET ";
? ? ? ? $sql .= implode(",",$set);
? ? ? ? $sql .= " WHERE ".$array['where'];
? ? ? ? // addLog(array('file_name'=>'sqlUpdate','content'=>$sql));
? ? ? ? //echo 44;exit;這里需要增加報(bào)錯(cuò)!!!!!!!!!!!!!
? ? ? ? return $this->exec($sql);
? ? }
? ? public function delete($array){
? ? ? ? $sql = "DELETE FROM {$array['table']} WHERE 1 ".$array['where'];
? ? ? ? // addLog(array('file_name'=>'sqlDelete','content'=>$sql));
? ? ? ? return $this->exec($sql);
? ? }
? ? private function addsla($data){
? ? ? ? if($this->is_addsla){
? ? ? ? ? ? return trim(addslashes($data));
? ? ? ? }
? ? ? ? return $data;
? ? }
? ? /* 輸出sql報(bào)錯(cuò)方法 */
? ? function showSqlError($array){
? ? ? ? die(PHP_EOL.$this->sql.PHP_EOL.$array['exception'].PHP_EOL);
? ? }
? ? /* 獲得表名 */
? ? function getTable(){
? ? ? ? $CDS = CDS_Controller::get_instance();
? ? ? ? if($CDS->table){
? ? ? ? ? ? return $CDS->table;
? ? ? ? }else{
? ? ? ? ? ? die('沒有輸入table表名');
? ? ? ? }
? ? }
? ? // public function addLog($Log)
? ? // {
? ? //? \file_put_contents($Log['file_name']."[".date("Y-m-d H:i:s")."] :".$Log['content']);
? ? // }
}