封装类:
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);
}
}