深圳市金黑网络技术有限公司始终坚持以用户需求为导向,提供安全、稳定、高效的产品和服务!
签到 · 搜索导航 · 服务热线 · 微信/手机:17817817816

深圳网站建设

开启左侧

php教程数据操作部分

[复制链接]
发表于 2022-12-10 21:06:35 | 442 | 0 | 显示全部楼层 |阅读模式
php教程数据操作部分

语法代码如下:
  1. <?php
  2. class MyPDO{
  3.     private $type;      //数据库类别
  4.     private $host;      //主机地址
  5.     private $port;      //端口号
  6.     private $dbname;    //数据库名
  7.     private $charset;   //字符集
  8.     private $user;      //用户名
  9.     private $pwd;       //密码
  10.     private $pdo;       //保存PDO对象
  11.     private static $instance;
  12.     private function __construct($param) {
  13.         $this->initParam($param);
  14.         $this->initPDO();
  15.         $this->initException();
  16.     }
  17.     private function __clone() {
  18.     }
  19.     public static function getInstance($param=array()){
  20.         if(!self::$instance instanceof self)
  21.             self::$instance=new self($param);
  22.         return self::$instance;
  23.     }
  24.     //初始化参数
  25.     private function initParam($param){
  26.         $this->type=$param['type']??'mysql';
  27.         $this->host=$param['host']??'127.0.0.1';
  28.         $this->port=$param['port']??'3306';
  29.         $this->dbname=$param['dbname']??'data';
  30.         $this->charset=$param['charset']??'utf8';
  31.         $this->user=$param['user']??'root';
  32.         $this->pwd=$param['pwd']??'root';
  33.     }
  34.     //初始化PDO
  35.     private function initPDO(){
  36.         try{
  37.             $dsn="{$this->type}:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}";
  38.             $this->pdo=new PDO($dsn, $this->user, $this->pwd);
  39.         } catch (PDOException $ex) {
  40.             $this->showException($ex);
  41.             exit;
  42.         }
  43.     }
  44.    
  45.     //显示异常
  46.     private function showException($ex,$sql=''){
  47.         if($sql!=''){
  48.             echo 'SQL语句执行失败<br>';
  49.             echo '错误的SQL语句是:'.$sql,'<br>';
  50.         }
  51.         echo '错误编号:'.$ex->getCode(),'<br>';
  52.         echo '错误行号:'.$ex->getLine(),'<br>';
  53.         echo '错误文件:'.$ex->getFile(),'<br>';
  54.         echo '错误信息:'.$ex->getMessage(),'<br>';
  55.     }
  56.     //设置异常模式
  57.     private function initException(){
  58.         $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  59.     }

  60.     //执行增、删、改操作
  61.     public function exec($sql){
  62.         try{
  63.             return $this->pdo->exec($sql);
  64.         } catch (PDOException $ex) {
  65.             $this->showException($ex, $sql);
  66.             exit;
  67.         }
  68.     }
  69.     //获取自动增长的编号
  70.     public function lastInsertId(){
  71.         return $this->pdo->lastInsertId();
  72.     }
  73. }
  74. //测试
  75. $param=array(
  76.    
  77. );
  78. $mypdo= MyPDO::getInstance($param);
  79. //echo $mypdo->exec('delete from news where id=6');
  80. if($mypdo->exec("insert into news values (null,'11','1111',unix_timestamp())"))
  81.     echo '自动增长的编号是:'.$mypdo->lastInsertId ();
复制代码

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

快速回复 返回顶部 返回列表