PDO,MySQL,MYSQLI的各自不同介绍,PDO,MYSQL,MYSQLI 性能哪个比较好
普通的mysql连接肯定是会被抛弃的 因为每次都要防止sql注入的问题 而且相对来说比较慢
首先, mysqli 连接是永久连接,而mysql是非永久连接 。什么意思呢? mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以很大程度的减轻服务器端压力。
mysqli是在普通mysql的基础上做的一次优化说实话很成功 预处理方式完全解决了sql注入的问题
但是唯一的不足点 就是只支持mysql数据库当然如果你要是不操作其他的数据库或者 当然这无疑是最好的选择
PDO则是最新出来的一种 连接方式 兼容大部分数据库 也解决了sql注入 但是也有缺点 它只支持php5.1以上的版本不过听说在未来的php6中 只支持这种连接.PDO统一所有数据库抽象层对象接口,mysqli只统一mysql的
简单说,PDO可以实现同样的代码对不同数据库的操作,例如你从mysql迁移到mssql,程序基本不需要改动
而mysqli简单理解未mysql的封装就好 在高负载的情况下.PDO开启长连接能够得到一个相对稳定的负载“值”。但是效率却不是最高的。 mysql最快。mysqli其次。只是mysql和mysqli在高并发、系统高负载的时候。其所承担的负载也是很可观的。PDO则不会。http://hudeyong926.iteye.com/blog/1824869
- <?php
-
-
-
- class Db
- {
- protected $db;
- protected $_sql;
- protected $throw = 1;
- protected $_query;
-
- function __construct($dbname = NULL, $port = '3306') {
- if (!$dbname) {
- $this->db = new mysqli(HOST, USER, PASS, DBNAME, PORT);
- } else {
- $this->db = new mysqli(HOST, USER, PASS, $dbname, $port);
- }
-
- $this->db->set_charset(CHARSET);
-
- if ($this->db->connect_error) {
- if ($this->throw) {
- throw new Exception($this->db->connect_error, $this->db->connect_errno);
- }
- }
-
- }
-
-
-
-
-
-
- public function field($field) {
- $this->_query['field'] = "SELECT {$field}";
- return $this;
- }
-
-
-
-
-
-
- public function table($table) {
- $this->_query['table'] = "FROM {$table}";
- return $this;
- }
-
-
-
-
-
-
- public function join($join) {
- $this->_query['join'][] = $join;
- return $this;
- }
-
-
-
-
-
-
- public function where($where) {
- $this->_query['where'] = "WHERE {$where}";
- return $this;
- }
-
-
-
-
-
-
- public function order($order) {
- if ($order != '') {
- $this->_query['order'] = "ORDER BY {$order}";
- }
- return $this;
- }
-
-
-
-
-
-
- public function limit($limit) {
- if ($limit != '') {
- $this->_query['limit'] = "LIMIT {$limit}";
- }
- return $this;
- }
-
-
-
-
-
- private function buildsql() {
-
- $sql = $this->_query['field'] . ' ' . $this->_query['table'];
-
- if (!empty($this->_query['join'])) {
- foreach ($this->_query['join'] as $join) {
- $sql .= " {$join}";
- };
- }
-
- if (isset($this->_query['del'])) {
- $sql = $this->_query['del'] . ' ' . $this->_query['table'];
- }
-
- if ($this->_query['where'] != '') {
- $sql .= ' ' . $this->_query['where'];
- }
-
- if (isset($this->_query['order']) && $this->_query['order'] != '') {
- $sql .= ' ' . $this->_query['order'];
- }
- unset($this->_query);
-
- return $sql;
-
- }
-
-
-
-
-
- public function select() {
- $sql = $this->buildsql();
-
- return $this->fetchAll($sql);
- }
-
-
-
-
-
-
- public function findNumRows() {
- $sql = $this->buildsql();
-
- $res = $this->query($sql);
-
- return $res->num_rows;
- }
-
-
-
-
-
- public function delRow() {
- $this->_query['del'] = "DELETE";
-
- $sql = $this->buildsql();
-
- $res = $this->query($sql);
-
- if ($res === FALSE) {
- return FALSE;
- }
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
- public function chkUnique($table, $where, $keyid = 'id') {
-
- $sql = "SELECT {$keyid} from {$table} WHERE {$where}";
-
- $num = $this->getNumRows($sql);
-
- if ($num > 0) {
- return FALSE;
- }
-
- return TRUE;
- }
-
-
-
-
-
- public function findRow() {
- $sql = $this->buildsql();
-
- return $this->fetchRow($sql);
- }
-
-
-
-
-
-
- public function query($sql) {
- $this->_sql = $sql;
- $res = $this->db->query($sql);
-
- if ($res === FALSE) {
- if ($this->throw) {
- throw new Exception("发生错误: 错误信息 {$this->getLastErr()} 相关sql语句 {$this->_sql}", $this->db->errno);
- } else {
- return FALSE;
- }
- }
-
- return $res;
- }
-
-
-
-
-
- public function setThrow($bool = FALSE) {
- $this->throw = $bool;
- }
-
-
-
-
-
-
- public function buildSqlfile($sqlfile) {
- $file = file($sqlfile);
-
- if ($file === FALSE || empty($file)) {
- return FALSE;
- }
-
- foreach ($file as $key => $val) {
- if (preg_match('/^(-|#)/', $val) || trim($val) == '') {
- continue;
- }
-
- $new[] = $val;
- }
-
- $sqls = split(';', join('', $new));
-
-
- foreach ($sqls as $sql) {
- $this->query($sql);
- }
-
- return TRUE;
- }
-
-
-
-
-
-
- public function fetchRow($sql) {
- $res = $this->query($sql);
- $result = @$res->fetch_assoc();
- return $result;
- }
-
-
-
-
-
-
- public function fetchAll($sql, $key = '') {
- $res = $this->query($sql);
-
- $result = array();
-
- while ($row = $res->fetch_assoc()) {
- if ($key) {
- $result [$row[$key]] = $row;
- } else {
- $result [] = $row;
- }
- }
-
- return $result;
- }
-
-
-
-
-
-
-
- public function getNumRows($sql) {
- $res = $this->query($sql);
-
- return $res->num_rows;
- }
-
-
-
-
- public function getLastId() {
- return $this->db->insert_id;
- }
-
-
-
-
- public function getLastErr() {
- return $this->db->error;
- }
-
-
-
-
-
-
- public function getLastSql() {
- return $this->_sql;
- }
-
-
-
-
-
-
- public function locktab($tabname, $mode = 'READ') {
- $this->query("LOCK TABLE {$tabname} {$mode}");
- return $this;
- }
-
-
-
-
- public function unlocktab() {
- $this->query("UNLOCK TABLES");
- }
-
-
-
-
- public function execlockquery() {
- $sql = $this->buildsql();
-
- }
-
-
-
-
-
-
-
- public function add($data, $table, $replace = false) {
- if (!is_array($data) || $table == '' || count($data) == 0) {
- return false;
- }
- $fields = $values = array();
-
- foreach($data as $key => $val)
- {
- $fields[] = "`{$table}`.`{$key}`";
- $values[] = is_numeric($val) ? $val : "'{$val}'";
- }
- $field = join(',', $fields);
- $value = join(',', $values);
- unset($fields, $values);
-
-
- $cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
- $sql = $cmd . ' `' . $table . '`(' . $field . ') VALUES (' . $value . ')';
- $this->query($sql);
-
- return $this->getLastId();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function update($table, $data, $where = '') {
- if ($table == '' or $where == '') {
- return false;
- }
-
- $where = ' WHERE ' . $where;
- $field = '';
- if (is_string($data) && $data != '') {
- $field = $data;
- } elseif (is_array($data) && count($data) > 0) {
- $fields = array();
- foreach ($data as $k => $v) {
- switch (substr($v, 0, 2)) {
- case '+=':
- $v = substr($v, 2);
- if (is_numeric($v)) {
- $fields[] = "`{$k}`=`{$k}` + $v";
- } else {
- continue;
- }
-
- break;
- case '-=':
- $v = substr($v, 2);
- if (is_numeric($v)) {
- $fields[] = "`{$k}`=`{$k}` - $v";
- } else {
- continue;
- }
- break;
- default:
- $fields[] = "`{$k}`= $v";
- }
- }
- $field = implode(',', $fields);
- } else {
- return false;
- }
-
- $sql = 'UPDATE `' . $table . '` SET ' . $field . $where;
- return $this->query($sql);
- }
-
-
-
-
-
-
-
-
- public function delete($table, $where = null) {
- if ($table == '') {
- return false;
- }
-
- $sql = 'DELETE FROM `' . $table . '`';
- if ($where) {
- $sql .= " WHERE {$where}";
- }
-
- return $this->query($sql);
- }
-
-
-
-
-
- public function autocommit($status = FALSE) {
- $this->db->autocommit($status);
- }
-
-
-
-
- public function commit() {
- $this->db->commit();
- }
-
-
-
-
- public function rollback() {
- $this->db->rollback();
- }
-
-
- public function __destruct() {
- $this->db->close();
- }
-
- }