簡(jiǎn)單易用的php數(shù)據(jù)庫(kù)pdo操作類(lèi)(curd?demo)
操作類(lèi)文件
db.class.php
<?php class DB_API { // 數(shù)據(jù)庫(kù)表名 protected $table; // 數(shù)據(jù)庫(kù)主鍵 protected $primary = 'id'; // 表前綴 protected $prefix = ''; // WHERE和ORDER拼裝后的條件 private $filter = array(); // PDO private $pdo; // PDOStatement private $Statement; // PDO鏈接數(shù)據(jù)庫(kù) public function __construct($config){ class_exists('PDO') or exit("not found PDO"); try{ $this->pdo = new PDO("mysql:host=".$config['db_host'].";port=".$config['db_port'].";dbname=".$config['db_name'],$config['db_user'], $config['db_pass']); }catch(PDOException $e){ // 數(shù)據(jù)庫(kù)無(wú)法鏈接,如果您是第一次使用,請(qǐng)先配置數(shù)據(jù)庫(kù)! exit($e->getMessage()); } $this->prefix = $config['db_prefix']; $this->pdo->exec("SET NAMES UTF8"); } // 配置表信息 public function set_table($table=null,$primary='id'){ if($table==null){ exit('Not found Table');} $this->primary = $primary; $this->table = $this->prefix.$table; return $this; } // 獲取數(shù)據(jù) public function getData($sql) { if(!$result = $this->query($sql))return array(); if(!$this->Statement->rowCount())return array(); $rows = array(); while($rows[] = $this->Statement->fetch(PDO::FETCH_ASSOC)){} $this->Statement=null; array_pop($rows); return $rows; } // 查詢數(shù)據(jù)條數(shù) public function getCount($conditions){ $where = ''; if(is_array($conditions)){ $join = array(); foreach( $conditions as $key => $value ){ $value = '\''.$value.'\''; $join[] = "{$key} = {$value}"; } $where = "WHERE ".join(" AND ",$join); }else{ if(null != $conditions)$where = "WHERE ".$conditions; } $sql = "SELECT count(*) as Frcount FROM {$this->table} {$where}"; $result = $this->getData($sql); return $result[0]['Frcount']; } // 獲取單一字段內(nèi)容 public function getField($where=null,$fields=null){ if( $record = $this->findAll($where, null, $fields, 1) ){ $res = array_pop($record); return $res[$fields]; }else{ return FALSE; } } // 遞增數(shù)據(jù) public function goInc($conditions,$field,$vp=1){ $where = ""; if(is_array($conditions)){ $join = array(); foreach( $conditions as $key => $value ){ $value = '\''.$value.'\''; $join[] = "{$key} = {$value}"; } $where = "WHERE ".join(" AND ",$join); }else{ if(null != $conditions)$where = "WHERE ".$conditions; } $values = "{$field} = {$field} + {$vp}"; $sql = "UPDATE {$this->table} SET {$values} {$where}"; return $this->pdo->exec($sql); } // 遞減 public function goDec($conditions,$field,$vp=1){ return $this->goInc($conditions,$field,-$vp); } // 修改數(shù)據(jù) public function update($conditions,$row) { $where = ""; $row = $this->__prepera_format($row); if(empty($row)){ return FALSE; } if(is_array($conditions)){ $join = array(); foreach( $conditions as $key => $condition ){ $condition = '\''.$condition.'\''; $join[] = "{$key} = {$condition}"; } $where = "WHERE ".join(" AND ",$join); }else{ if(null != $conditions){ $where = "WHERE ".$conditions; } } foreach($row as $key => $value){ $value = '\''.$value.'\''; $vals[] = "{$key} = {$value}"; } $values = join(", ",$vals); $sql = "UPDATE {$this->table} SET {$values} {$where}"; //echo $sql.'<br/>'; $res = $this->pdo->exec($sql); if($res){ return $res; }else{ var_dump($this->pdo->errorInfo()); } } // 查詢所有 public function findAll($conditions=null,$order=null,$fields=null,$limit=null) { $where = ''; if(is_array($conditions)){ $join = array(); foreach( $conditions as $key => $value ){ $value = '\''.$value.'\''; $join[] = "{$key} = {$value}"; } $where = "WHERE ".join(" AND ",$join); }else{ if(null != $conditions)$where = "WHERE ".$conditions; } if(is_array($order)){ $where .= ' ORDER BY '; $where .= implode(',', $order); }else{ if($order!=null)$where .= " ORDER BY ".$order; } if(!empty($limit))$where .= " LIMIT {$limit}"; $fields = empty($fields) ? "*" : $fields; $sql = "SELECT {$fields} FROM {$this->table} {$where}"; return $this->getData($sql); } // 查詢一條 public function find($where=null,$order=null,$fields=null,$limit=1) { if( $record = $this->findAll($where, $order, $fields, 1) ){ return array_pop($record); }else{ return FALSE; } } // 執(zhí)行SQL語(yǔ)句并檢查是否錯(cuò)誤 public function query($sql){ $this->filter[] = $sql; $this->Statement = $this->pdo->query($sql); if ($this->Statement) { return $this; }else{ $msg = $this->pdo->errorInfo(); if($msg[2]) exit('數(shù)據(jù)庫(kù)錯(cuò)誤:' . $msg[2] . end($this->filter)); } } // 執(zhí)行SQL語(yǔ)句函數(shù) public function findSql($sql) { return $this->getData($sql); } // 根據(jù)條件 (conditions) 刪除 public function delete($conditions) { $where = ""; if(is_array($conditions)){ $join = array(); foreach( $conditions as $key => $condition ){ $condition = '\''.$condition.'\''; $join[] = "{$key} = {$condition}"; } $where = "WHERE ( ".join(" AND ",$join). ")"; }else{ if(null != $conditions)$where = "WHERE ( ".$conditions. ")"; } $sql = "DELETE FROM {$this->table} {$where}"; return $this->pdo->exec($sql); } // 新增數(shù)據(jù) public function add($row) { if(!is_array($row)){ return FALSE; } $row = $this->__prepera_format($row); if(empty($row)){ return FALSE; } foreach($row as $key => $value){ $cols[] = $key; $vals[] = '\''.$value.'\''; } $col = join(',', $cols); $val = join(',', $vals); $sql = "INSERT INTO {$this->table} ({$col}) VALUES ({$val})"; if( FALSE != $this->pdo->exec($sql) ){ if( $newinserid = $this->pdo->lastInsertId() ){ return $newinserid; }else{ $a=$this->find($row, "{$this->primary} DESC",$this->primary); return array_pop($a); } } return FALSE; } private function __prepera_format($rows) { $stmt = $this->pdo->prepare('DESC '.$this->table); $stmt->execute(); $columns = $stmt->fetchAll(PDO::FETCH_COLUMN); $newcol = array(); foreach( $columns as $col ){ $newcol[$col] = null; } return array_intersect_key($rows,$newcol); } }
實(shí)例化類(lèi)
<?php header("Content-type:text/html;charset=utf-8"); // 引入類(lèi) include './db.class.php'; // 數(shù)據(jù)庫(kù)配置 $config = [ 'db_host' => 'localhost', // 數(shù)據(jù)庫(kù)地址 'db_port' => 3306, // 默認(rèn)mysql數(shù)據(jù)庫(kù)端口 'db_name' => 'test', // 數(shù)據(jù)庫(kù)名字 'db_user' => 'root', // 數(shù)據(jù)庫(kù)用戶名 'db_pass' => 'root', // 數(shù)據(jù)庫(kù)密碼 'db_prefix' => '', // 表前綴 ]; // 實(shí)例化類(lèi) $db = new DB_API($config); // 表名 $article = $db->set_table('article'); // 新增數(shù)據(jù) $newdata = ['title'=>'this is a title']; $r = $article->add($newdata); if($r){ echo '新增成功!'; }else{ echo '操作失?。?; } // 查詢數(shù)據(jù) $where = ['id'=>3]; $find = $article->find($where); //查詢一條數(shù)據(jù) $find = $article->findAll($where); // 查詢多條數(shù)據(jù) print_r($find); // 更新數(shù)據(jù) $updataData = ['title'=>'hello world is updated']; // 要修改的數(shù)據(jù) $where = ['id'=>1]; // 條件 $update = $article->update($where, $updataData); if($update ){ echo '更新成功!'; // 查詢并打印 $newdata = $article->find('id=1'); print_r($newdata); }else{ echo '更新失敗!'; } // 刪除數(shù)據(jù) $where = ['id'=>1]; $del = $article->delete($where); if($del){ echo '刪除成功!'; }else{ echo '刪除失?。?; } // 獲取符合條件的記錄數(shù) $where = ['author'=>'TANKING']; $count = $article->getCount($where); echo $count; // 執(zhí)行原生SQL語(yǔ)句 $sql = 'select * from article where id=3'; $lists = $article->findSql($sql); print_r($lists); // 根據(jù)條件查詢出對(duì)應(yīng)的字段的值 $where = ['id'=>1]; $res = $article->getField($where,'title'); if ($res) { echo $res; }else{ echo "沒(méi)有數(shù)據(jù)"; } // 高級(jí)查詢 // $conditions查詢條件 // $order排序方法 // $fields指定字段 // $limit查詢條數(shù) $res = $article->findAll($conditions=null,$order='id asc',$fields=null,$limit=null); if ($res) { print_r($res); }else{ print_r("沒(méi)有數(shù)據(jù)"); }
以上就是簡(jiǎn)單易用的php數(shù)據(jù)庫(kù)pdo操作類(lèi)(curd demo)的詳細(xì)內(nèi)容,更多關(guān)于php數(shù)據(jù)庫(kù)pdo操作類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php統(tǒng)計(jì)時(shí)間和內(nèi)存使用情況示例分享
這篇文章主要介紹了php統(tǒng)計(jì)時(shí)間和內(nèi)存使用情況示例,大家直接調(diào)用下面的方法就可以使用,需要的朋友可以參考下2014-03-03PHP file_get_contents函數(shù)讀取遠(yuǎn)程數(shù)據(jù)超時(shí)的解決方法
這篇文章主要介紹了PHP file_get_contents函數(shù)讀取遠(yuǎn)程數(shù)據(jù)超時(shí)的解決方法,本文直接給出解決方法代碼,需要的朋友可以參考下2015-05-05通過(guò)curl模擬post和get方式提交的表單類(lèi)
寫(xiě)了個(gè)簡(jiǎn)單的腳本通過(guò)curl的方式模擬表單提交??梢酝ㄟ^(guò)數(shù)組和字符串兩種方式提交數(shù)據(jù),需要的朋友可以參考下2014-04-04IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安裝實(shí)例(已經(jīng)完成
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安裝實(shí)例(已經(jīng)完成)...2006-12-12詳解thinkphp5+swoole實(shí)現(xiàn)異步郵件群發(fā)(SMTP方式)
這篇文章主要介紹了詳解thinkphp5+swoole實(shí)現(xiàn)異步郵件群發(fā)(SMTP方式),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Zend Framework使用Zend_Loader組件動(dòng)態(tài)加載文件和類(lèi)用法詳解
這篇文章主要介紹了Zend Framework使用Zend_Loader組件動(dòng)態(tài)加載文件和類(lèi)用法,結(jié)合實(shí)例形式分析了Zend_Loader組件實(shí)現(xiàn)文件自動(dòng)加載與屬性判斷的相關(guān)使用技巧,需要的朋友可以參考下2016-12-12PHP中使用substr()截取字符串出現(xiàn)中文亂碼問(wèn)題該怎么辦
本文給大家介紹使用php substr()截取字符串出現(xiàn)亂碼問(wèn)題該怎么辦,涉及到php substr()方法的一些知識(shí)點(diǎn),感興趣的朋友一起學(xué)習(xí)下吧2015-10-10