预处理操作crud

Original 2019-03-16 10:06:11 384
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';

//参数绑定


$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);

//参数绑定

$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:写的不错。现在数据库操作,都使用pdo了,所以要多练习,继续加油。

Release Notes

Popular Entries