abstract:<?php class Curd { public $pdo = ''; public function __construct(){ $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=1604a','root','root'); } public
<?php
class Curd
{
public $pdo = '';
public function __construct(){
$this->pdo = new PDO('mysql:host=127.0.0.1;dbname=1604a','root','root');
}
public function show(){//此方法為展示方法
$sql = "SELECT * FROM `userl` where user_id > :user_id";
$sie = $this->pdo->prepare($sql);
$sie->execute([':user_id'=>1]);
$aaa[] = '';
while ($row =$sie->fetch(PDO::FETCH_ASSOC)) {
$aaa[] = $row;
}
var_dump($aaa);
}
public function a(){
echo '你沒有傳遞方法名所以被迫跳到此頁面';
}
//add()//此為添加方法
public function add(){
$sql = 'INSERT INTO `userl` (`name`, `password`, `state`, `creationTime`) VALUES (:name, :password,:state ,:creationTime)';
$sie = $this->pdo->prepare($sql);
$name = '王大大';
$password = '12333123';
$state = 0;
$creationTime = '2019-03-15 14:13:52';
$sie->bindParam(':name', $name, PDO::PARAM_STR,100);
$sie->bindParam(':password', $password, PDO::PARAM_STR,100);
$sie->bindParam(':state', $state, PDO::PARAM_INT);
$sie->bindParam(':creationTime', $creationTime, PDO::PARAM_STR,100);
if ($sie->execute()) {
echo ($sie->rowCount()>0)?'當前添加了'.$sie->rowCount().'條記錄':'沒有添加記錄';
}else{
exit($sie->errorInfo());
}
}
public function upl(){//此方法為修改方法
$sql = 'UPDATE `userl` SET `name`=:name, `password`=:password, `state`=:state, `creationTime`= :creationTime WHERE `user_id`=:user_id';
$sie = $this->pdo->prepare($sql);
$user_id = 2;
$name = '王小曉';
$password = '123331';
$state = 0;
$creationTime = '2019-03-15 14:13:52';
//參數(shù)綁定
$sie->bindParam(':user_id',$user_id,PDO::PARAM_INT);
$sie->bindParam(':name', $name, PDO::PARAM_STR,100);
$sie->bindParam(':password', $password, PDO::PARAM_STR,100);
$sie->bindParam(':state', $state, PDO::PARAM_INT);
$sie->bindParam(':creationTime', $creationTime, PDO::PARAM_STR,100);
if ($sie->execute()) {
echo ($sie->rowCount()>0)?'當前有'.$sie->rowCount().'條記錄被修改':'沒有記錄被修改';
}else{
exit($sie->errorInfo());
}
}
public function del(){//此方法為刪除方法
$sql = 'DELETE FROM `userl` WHERE (`user_id`=:user_id)';
$sie = $this->pdo->prepare($sql);
//參數(shù)綁定
$user_id = 1;
$sie->bindParam(':user_id',$user_id,PDO::PARAM_INT);
if ($sie->execute()) {
echo ($sie->rowCount()>0)?'當前有'.$sie->rowCount().'條記錄被刪除':'沒有記錄被刪除';
}else{
exit($sie->errorInfo());
}
}
}
$curd = new Curd();
$r = isset($_GET['r'])?$_GET['r']:'a';
$curd->$r();
Correcting teacher:查無此人Correction time:2019-03-16 10:44:55
Teacher's summary:寫的不錯。現(xiàn)在數(shù)據(jù)庫操作,都使用pdo了,所以要多練習,繼續(xù)加油。