PDO操作MySQL增刪改查封裝的類


封裝類:

<?php
class PDOClass{
protected $pdo;
//構造函數
public function __construct()
{
$this -> pdo = new PDO('mysql:host=127.0.0.1;dbname=seven_month','root','root',array(PDO::ATTR_PERSISTENT));
}
//添加
public function add($data){
$res = $this -> pdo -> prepare("insert into seven_day values (null,?,?,?,?)");
return $res -> execute($data);
}
//查詢所有
public function select_All($value){
if (empty($value)){
return $this -> pdo -> query('select * from seven_day') -> fetchAll(PDO::FETCH_ASSOC);
}else{
$res = $this -> pdo -> prepare("select * from seven_day like :value");
$res -> bindValue(':value','%'.$value.'%');
$res -> execute();
return $res -> fetchAll(PDO::FETCH_ASSOC);
}
}
//查詢一條
public function select_One($id){
return $this -> pdo -> query("select * from seven_day where id = $id") -> fetch(PDO::FETCH_ASSOC);
}
//刪除數據
public function del($id){
return $this -> pdo -> exec("delete from seven_day where id = $id");
}
//修改數據
public function update($data){
$res = $this -> pdo -> prepare("update seven_day set name = ? where id = ?");
return $res -> execute($data);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM