PDO(PHP Data Object)扩展为PHP访问各种数据库提供了一个轻量级,一致性的接口。无论访问什么数据库,都可以通过一致性的接口去操作。
在php.ini中开启 PDO的扩展
1 2 | 开启PDO连接MySQL扩展 extension=php_pdo_mysql.dll |
PDO的单例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | class MyPDO { private $type; //数据库类别 private $host; //主机地址 private $port; //端口号 private $dbname; //数据库名 private $charset; //字符集 private $user; //用户名 private $pwd; //密码 private $pdo; //保存PDO对象 private static $instance; private function __construct($param) { $this->initParam($param); $this->initPDO(); $this->initException(); } private function __clone() { // TODO: Implement __clone() method. } public static function getInstance($param = array()) { if (!self::$instance instanceof self) { self::$instance = new self($param); } return self::$instance; } private function initParam($param) { // $this->type = $param['type'] ? $param['type'] : 'mysql'; // $this->host = $param['host'] ? $param['host'] : '127.0.0.1'; // $this->port = $param['port'] ? $param['port'] : '3306'; // $this->dbname = $param['dbname'] ? $param['dbname'] : 'market'; // $this->charset = $param['charset'] ? $param['charset'] : 'utf8'; // $this->user = $param['user'] ? $param['user'] : 'root'; // $this->pwd = $param['pwd'] ? $param['pwd'] : 'root'; $this->type = $param['type'] ??'mysql'; $this->host = $param['host'] ??'127.0.0.1'; $this->port = $param['port'] ??'3306'; $this->dbname = $param['dbname'] ??'market'; $this->charset = $param['charset'] ??'utf8'; $this->user = $param['user'] ??'root'; $this->pwd = $param['pwd'] ??'root'; } private function initPDO() { try { $dsn = "{$this->type}:host={$this->host};port={$this->pwd};dbname={$this->dbname};charset={$this->charset}"; $this->pdo = new PDO($dsn, $this->user, $this->pwd); } catch (PDOException $e) { $this->showException($e); exit(); } } //显示异常 private function showException($ex, $sql = '') { if ($sql != '') { echo 'SQL语句执行失败<br>'; echo '错误的SQL语句是:' . $sql, '<br>'; } echo '错误编号:' . $ex->getCode(), '<br>'; echo '错误行号:' . $ex->getLine(), '<br>'; echo '错误文件:' . $ex->getFile(), '<br>'; echo '错误信息:' . $ex->getMessage(), '<br>'; } //设置异常模式 private function initException() { $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } //增删改操作 public function exec($sql) { try { return $this->pdo->exec($sql); } catch (PDOException $ex) { $this->showException($ex, $sql); exit; } } //获取自动增长的编号 public function lastInsertId() { return $this->pdo->lastInsertId(); } //判断匹配的类型 private function fetchType($type) { switch ($type) { case 'num'; return PDO::FETCH_NUM; case 'both': return PDO::FETCH_BOTH; case 'obj': return PDO::FETCH_OBJ; default: return PDO::FETCH_ASSOC; } } //获取所有数据,返回二维数组 public function fetchAll($sql,$type='assoc'){ try { $stmt = $this->pdo->query($sql);//获取PDOStatement对象 $type = $this->fetchType($type);//获取匹配方法 return $stmt->fetchAll($type); } catch (Exception $e) { $this->showException($e, $sql); } } //获取一位数组 public function fetchRow($sql,$type='assoc'){ try { $stmt = $this->pdo->query($sql); $type = $this->fetchType($type); return $stmt->fetch($type); } catch (Exception $e) { $this->showException($e, $sql); exit; } } //返回一行一列 public function fetchColumn($sql){ try { $stmt = $this->pdo->query($sql); return $stmt->fetchColumn(); } catch (Exception $e) { $this->showException($e, $sql); exit; } } } |
使用的例子
1 2 3 4 5 6 7 8 9 10 11 12 | $param = array(); //$param=['type'=>'mysql','host'=>'127.0.0.1','port'=>'3306','user'=>'root','pwd'=>'root','dbname'=>'market','charset'=>'utf8','user'=>'root','pwd'=>'root']; $myPDO = MyPDO::getInstance($param); //$sql = "insert into test values (null ,'小天','南京')"; //$rs = $myPDO->exec($sql); //if ($rs) { // echo $myPDO->lastInsertId(); //} $sql="select *from test "; $all = $myPDO->fetchAll($sql, 'num'); echo '<pre>'; print_r($all) ; |