实例
<?php /医院 * 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';
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /医院 * 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; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /医院 * 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);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /医院 * 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);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /医院 * 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 "<pre>".print_r($pdo->Select($table,$data,$where,$order),true)."</pre>";
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /医院 * 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);
运行实例 »
点击 "运行实例" 按钮查看在线实例