PHP中PDO数据库模型
<?php
/**
* PDO数据库模型
* @author LiZeQiao <674531003@qq.com>
* @version
*/
class Model{
public $dbType=’mysql’; //数据库类型
public $dbHost=’localhost’; //主机名
public $dbName=’mywordpress’; //数据库名
public $dbPort=’3306′; //数据库端口
public $dbUser=’root’; //数据库用户名
public $dbPwd=’root’; //数据库密码
public $tableName=”; //表名
public $pdo; //pdo的对象
public $fields=array(); //表的所有字段
public $priKey; //主键
public $where=”;
public $group=”;
public $having=”;
public $order=”;
//构造函数
public function __construct($tableName=”){
$this->tableName=$tableName;
$this->connect();
if(!empty($this->tableName)){
$this->getFields(); //关于
}
}
//初始化数据库
public function connect(){
try{
$dsn=”{$this->dbType}:dbhost={$this->dbHost};dbname={$this->dbName};port={$this->dbPort}”;
$this->pdo=new PDO($dsn,$this->dbUser,$this->dbPwd);
}catch(PDOException $ex){
echo $ex->getMessage();
exit;
}
$this->pdo->exec(‘set names utf8′);
//$this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}
//获取当前表的所有字段
public function getFields(){
$stmt=$this->pdo->query(“desc {$this->tableName}”);
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$fields[]=$row['Field'];
if($row['Key']==’PRI’){
$this->priKey=$row['Field'];
}
}
$this->fields=$fields;
}
//获取所有记录信息
public function select(){
$sql=”select * from {$this->tableName} {$this->where} {$this->order} “;
$stmt=$this->pdo->prepare($sql);
if($stmt->execute()){
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}else{
return false;
}
}
//增加一条记录
public function insert($data=array()){
foreach($data as $k=>$v){
if(in_array($k,$this->fields)){
$keys .= $k . ‘,’;
$keyss .= ‘:’ . $k . ‘,’;
$arr[$k]=$v;
}
}
$keys=rtrim($keys,’,’);
$keyss=rtrim($keyss,’,’); //拼接成:name,:age,:email execute($arr)
$sql=”insert into {$this->tableName} ($keys) values($keyss)”;
$stmt=$this->pdo->prepare($sql);
if($stmt->execute($arr)){
return $this->pdo->lastInsertId();
}else{
return false;
}
}
//查找一条记录
public function find($id){
$stmt=$this->pdo->prepare(“select * from {$this->tableName} where {$this->priKey}= ? “);
$stmt->bindParam(1,$id);
if($stmt->execute()){
return $stmt->fetch(PDO::FETCH_ASSOC);
}else{
return false;
}
}
//更新一条记录
public function update($data=array(),$id){
foreach($data as $k=>$v){
if(in_array($k,$this->fields)){
$sets .= “$k=:$k,”;
$arr[$k]=$v;
}
}
$sets=rtrim($sets,’,’);
$sql=”update {$this->tableName} set $sets where {$this->priKey} = $id”;
$stmt=$this->pdo->prepare($sql);
if($stmt->execute($arr)){
return $id;
}else{
return false;
}
}
//删除一条记录
public function delete($id){
$stmt=$this->pdo->prepare(“delete from {$this->tableName} where {$this->priKey}=:id “);
$stmt->bindParam(‘:id’,$id);
if($stmt->execute()){
return $id;
}else{
return false;
}
}
//where
public function where($where=”){
if(!empty($where)){
$this->where=’ where ‘ . $where;
}
return $this;
}
//group
public function group($group=”){
if(!empty($group)){
$this->group=’ group by ‘ . $group;
}
return $this;
}
//having
public function having($having=”){
if(!empty($having)){
$this->having=’ having ‘ . $having;
}
return $this;
}
//order
public function order($order=”){
if(!empty($order)){
$this->order=’ order by ‘ . $order;
}
return $this;
}
}
- PHP数据库模型
- PHP分页类