PDO方式封装数据库类(仅供参考)
public function connect()
{
$dsn = $this->config['type'].":host=".$this->config['host'].";dbname=".$this->config['databasename'];
$this->pdo = new PDO($dsn,$this->config['user'],$this->config['pwd']);
return $this->pdo;
}
/**
* @param $param
* @return $this
* 设置表名
*/
public function table($param)
{
$this->table = $param;
return $this;
}
public function where($where)
{
$this->where = $where;
return $this;
}
/**
* 查询所有的mysqli方式
*/
public function all()
{
$sql = "select * from ".$this->table." where ".$this->where;
// $res = $this->pdo->query($sql)->fetchAll();
/**
* PDO设置
*/
if($this->getresult == "num"){
$res = $this->pdo->query($sql)->fetchAll(PDO::FETCH_NUM);
}elseif($this->getresult == "assoc"){
$res = $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}else{
$res = $this->pdo->query($sql)->fetchAll();
}
return $res;
}
/**
* @return array|null
* 查询单条数据 mysqli方式
*/
public function get()
{
$sql = "select * from ".$this->table." where ".$this->where;
/**
* PDO设置
*/
if($this->getresult == "num"){
$res = $this->pdo->query($sql)->fetch(PDO::FETCH_NUM);
}elseif($this->getresult == "assoc"){
$res = $this->pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
}else{
$res = $this->pdo->query($sql)->fetch();
}
return $res;
}
/**
* @content PDO
*/
public function delete()
{
$sql = 'delete from '.$this->table .$this->where;
$res = $this->pdo->prepare($sql);
$id = 9;
$res->bindValue(1,$id);
$res->execute();
$affect_row = $res->rowCount();
return $affect_row;
}
/**
* 关联数据
*/
public function getnum()
{
$this->getresult = "num";
return $this;
}
/**
* 索引数组
*/
public function getassoc()
{
$this->getresult = "assoc";
return $this;
}