?php
//引入配置文件
include "../Config/config.php";
class Model extends PDO
{
protected $tableName = "";//存儲表名
protected $sql = "";//存儲最后執行的SQL語句
protected $limit = "";//存儲limit條件
protected $order = "";//存儲order排序條件
protected $field = "*";//存儲要查詢的字段
protected $where = "";//存儲where條件
protected $allFields = [];//存儲當前表的所有字段
/**
* 構造方法 初始化
* @param string $tableName 要操作的表名
*/
public function __construct($tableName)
{
//連接數據庫
parent::__construct('mysql:host='.HOST.';dbname='.DB.';charset=utf8;port='.PORT,USER,PWD);
//存儲表名
$this->tableName = PRE.$tableName;
//獲取當前數據表中有哪些字段
$this->getFields();
}
/**
* 獲取當前表的所有字段
* @return array 成功則返回一維數組字段
*/
public function getFields()
{
//查看當前表結構
$sql = "desc {$this->tableName}";
$res = $this->query($sql);//返回pdo對象
//var_dump($res);
if ($res) {
$arr = $res->fetchAll(2);
//var_dump($arr);
//從二維數組中取出指定下標的列
$this->allFields = array_column($arr,"Field");
return $this->allFields;
} else {
die("表名錯誤");
}
}
/**
* 添加操作
* @param array $data 要添加的數組
* @return int 返回受影響行數
*/
public function add($data)
{
//判斷是否是數組
if (!is_array($data)) {
return $this;
}
//判斷是否全是非法字段
if (empty($data)) {
die("非法字段");
}
//過濾非法字段
foreach($data as $k => $v){
if (!in_array($k,$this->allFields)) {
unset($data[$k]);
}
}
//將數組中的鍵取出
$keys = array_keys($data);
//將數組中取出的鍵轉為字符串拼接
$key = implode(",",$keys);
//將數組中的值轉化為字符串拼接
$value = implode("','",$data);
//準備SQL語句
$sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
$this->sql = $sql;
//執行并發送SQL,返回受影響行數
return (int)$this->exec($sql);
}
/**
* 刪除操作
* @param string $id 要刪除的id
* @return int 返回受影響行數
*/
public function delete($id="")
{
//判斷id是否存在
if (empty($id)) {
$where = $this->where;
}else{
$where = "where id={$id}";
}
$sql = "delete from {$this->tableName} {$where}";
$this->sql = $sql;
//執行并發送SQL,返回受影響行數
return (int)$this->exec($sql);
}
/**
* 修改操作
* @param array $data 要修改的數組
* @return int 返回受影響行數
*/
public function update($data)
{
//判斷是否是數組
if (!is_array($data)){
return $this;
}
//判斷是否全是非法字段
if (empty($data)) {
die('全是非法字段');
}
$str = "";
//過濾非法字段
foreach ($data as $k=>$v) {
//字段為id時,判斷id是否存在的
if ($k == "id"){
$this->where = "where id={$v}";
unset($data[$k]);
continue;
}
//若字段不為id,則過濾后再拼接成set字段
if (in_array($k, $this->allFields)) {
$str .= "{$k}='{$v}',";
} else {
unset($data[$k]);
}
}
//判斷是否傳了條件
if (empty($this->where)) {
die('請傳入修改條件');
}
//去除右邊的,
$str = rtrim($str, ',');
$sql = "update {$this->tableName} set {$str} {$this->where}";
//echo $sql;
$this->sql = $sql;
return (int)$this->exec($sql);
}
/**
* 查詢多條數據
* @return array 成功返回二維數組,失敗返回空數組
*/
public function select()
{
$sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
$this->sql = $sql;
//執行SQL,結果集是一個對象
$res = $this->query($sql);
//判斷是否查詢成功,
if ($res){
//成功返回二維數組
return $res->fetchAll(2);
}
//失敗返回空數組
return [];
}
/**
* 查詢一條數組
* @param string $id 要查詢的id
* @return array 返回一條數據
*/
public function find($id="")
{
//判斷是否存在id
if (empty($id)){
$where = $this->where;
}else{
$where = "where id={$id}";
}
$sql = "select {$this->field} from {$this->tableName} {$where} {$this->order} limit 1";
$this->sql = $sql;
//執行sql,結果集為對象
$res = $this->query($sql);
//判斷是否查詢成功
if ($res){
//成功則返回一條數據(一維數組)
$result = $res->fetchAll(2);
return $result[0];
}
//失敗返回空數組
return [];
}
/**
* 統計總數目
* @return int 返回總數
*/
public function count()
{
$sql = "select count(*) as total from {$this->tableName} {$this->where} limit 1";
$this->sql = $sql;
//執行SQL,結果集為對象
$res = $this->query($sql);
//處理結果集
if ($res){
$result = $res->fetchAll(2);
//var_dump($result);
return $result[0]["total"];
}
return 0;
}
/**
* 設置要查詢的字段信息
* @param string $field 要查詢的字段
* @return object 返回自己,保證連貫操作
*/
public function field($field)
{
//判斷字段是否存在
if (empty($filed)){
return $this;
}
$this->field = $field;
return $this;
}
/**
* 獲取最后執行的sql語句
* @return string sql語句
*/
public function _sql()
{
return $this->sql;
}
/**
* where條件
* @param string $where 要輸入的where條件
* @return object 返回自己,保證連貫操作
*/
public function where($where)
{
$this->where = "where ".$where;
return $this;
}
/**
* order條件
* @param string $order 要輸入的order條件
* @return object 返回自己,保證連貫操作
*/
public function order($order)
{
$this->order = "order by ".$order;
return $this;
}
/**
* limit條件
* @param string $limit 要輸入的limit條件
* @return object 返回自己,保證連貫操作
*/
public function limit($limit)
{
$this->limit = "limit ".$limit;
return $this;
}
}
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP基于pdo操作數據庫技巧總結》、《php+mysqli數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》