返回 预处理操作cr...... 登陆

预处理操作crud

弃。 2019-03-16 10:06:11 298

<?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();


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网