PHP封装curd,php之 PDO(CURD) 封装——>0426

030ef5790c442647cde419dc3cb95e1c.gif

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 2:06

*/

header("content-type:text/html;charset=utf-8");

$dsn = 'mysql:host=localhost;dbname=testguest';

$dbname = 'root';

$dbpass = 'root';

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 12:56

*/

header("content-type:text/html;charset=utf-8");

/医院

* Class PdoUtil pdo封装

*/

class PdoUtil

{

private $pdo;

private $dsn;

private $dbName;

private $dbPass;

private $option = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  //设置错误模式

PDO::ATTR_CASE => PDO::CASE_NATURAL,  //数据表字段保持不变

PDO::ATTR_EMULATE_PREPARES => true, //启用PDO模拟

PDO::ATTR_PERSISTENT => true, //启用持久性连接

];

public $stmt;

function PdoUtil($dsn, $dbName, $dbPass)

{

$this->dsn = $dsn;

$this->dbName = $dbName;

$this->dbPass = $dbPass;

try {

$this->pdo = new PDO($this->dsn, $this->dbName, $this->dbPass, $this->option);

} catch (PDOException $e) {

die("连接失败" . $e->getMessage());

}

}

/医院

* Insert

* @param $table

* @param array $data

*/

public function Insert($table, $data = [])

{

$sql = "INSERT IGNORE {$table} SET ";

foreach (array_keys($data) as $filed) {

$sql .= $filed . "=:" . $filed . ", ";

}

//去掉尾部逗号,并添加分号结束

$sql = rtrim(trim($sql), ',') . ';';

$this->stmt = $this->Pre($sql);

//绑定值到预处理对象

foreach ($data as $filed => $value) {

$this->stmt->bindValue(":{$filed}", $value);

}

if ($this->stmt->execute()) {

if ($this->stmt->rowCount() > 0) {

echo "最后插入的id:=" . $this->pdo->lastInsertId();

}

} else {

echo "插入失败";

}

}

/医院

* Update

* @param $table

* @param array $data

* @param $where

* @return bool

*/

public function Update($table, $data = [], $where)

{

$sql = "UPDATE {$table} SET ";

foreach (array_keys($data) as $filed) {

$sql .= $filed . "=:" . $filed . ", ";

}

$sql = rtrim(trim($sql), ',');

if (empty($where)) {

return false;

} else {

$sql .= ' WHERE ' . $where;

}

$this->stmt = $this->Pre($sql);

//绑定值到预处理对象

foreach ($data as $filed => $value) {

$this->stmt->bindValue(":{$filed}", $value);

}

if ($this->stmt->execute()) {

if ($this->stmt->rowCount() > 0) {

echo "更新受影响的函数:=" . $this->stmt->rowCount();

}

} else {

echo "更新失败";

}

}

/医院

* Select

* @param $table

* @param $fields

* @param string $where

* @param string $order

* @return array

*/

public function Select($table, $fields, $where = '', $order = '')

{

$sql = "SELECT ";

if (is_array($fields)) {

foreach ($fields as $field) {

$sql .= $field . ', ';

}

} else {

$sql .= $fields;

}

$sql = rtrim(trim($sql), ',');

$sql .= '  FROM ' . $table;

if (!empty($where)) {

$sql .= '  WHERE ' . $where;

}

if (!empty($order)) {

$sql .= ' order by ' . $order;

}

$this->stmt = $this->Pre($sql);

if ($this->stmt->execute()) {

if ($this->stmt->rowCount() > 0) {

$this->stmt->setFetchMode(PDO::FETCH_ASSOC);

//返回一个二维数组

return $this->stmt->fetchAll();

}

}

}

/医院

* Delete

* @param $table

* @param $where

* @return bool|string

*/

public function Delete($table, $where)

{

$sql = "DELETE FROM {$table} WHERE ";

if (empty($where)) {

return false;

} else {

$sql .= $where . ";";

}

$this->stmt= $this->Pre($sql);

if ($this->stmt->execute()){

if ($this->stmt->rowCount()>0){

return "删除".$this->stmt->rowCount()."条记录";

}else{

return "删除".$this->stmt->rowCount()."条记录";

}

}else{

return "删除失败";

}

}

/医院

* prepare

* @param $sql

* @return PDOStatement

*/

public function Pre($sql){

$this->stmt = $this->pdo->prepare($sql);

return $this->stmt;

}

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 2:04

*/

require "common/localHost.php";

require "library/PdoUtil.php";

$pdo = new PdoUtil($dsn,$dbname,$dbpass);

$table = "tg_user";

$data= ["dg_username"=>"admin".time(),"dg_password"=>sha1(1234567),"dg_question"=>"啊啊啊啊啊!!!","dg_answer"=>"啊什么啊?"];

$pdo->Insert($table,$data);

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 2:24

*/

require "common/localHost.php";

require "library/PdoUtil.php";

$pdo = new PdoUtil($dsn,$dbname,$dbpass);

$table = "tg_user";

$data= ["dg_username"=>"admin".time(),"dg_password"=>sha1(1234567),"dg_question"=>"刷新啊 !!!","dg_answer"=>"啊什么啊?"];

$where = "dg_id=3";

$pdo->Update($table,$data,$where);

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 2:42

*/

require "common/localHost.php";

require "library/PdoUtil.php";

$pdo = new PdoUtil($dsn,$dbname,$dbpass);

$table = "tg_user";

$data=["dg_username","dg_password","dg_question"];

//$data = "*";

$data = "*";

$where = "dg_id > 0";

$order = " dg_id DESC";

/医院

* 查询全部 where 条件不传就好了,不排序 order不传

*/

echo "

".print_r($pdo->Select($table,$data,$where,$order),true)."
";

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

/医院

* Created by PhpStorm.

* User: Administrator

* Date: 2018/4/27 0027

* Time: 下午 2:49

*/

require "common/localHost.php";

require "library/PdoUtil.php";

$pdo = new PdoUtil($dsn,$dbname,$dbpass);

$table = "tg_user";

$where = " dg_id = 3";

echo $pdo->Delete($table,$where);

运行实例 »

点击 "运行实例" 按钮查看在线实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值