代码拉取完成,页面将自动刷新
<?php
class CTG_DBLib { protected $pdo; protected $statement; protected $sql; protected $whereStr = ''; protected $valuesArr = array (); protected $table; protected $selectStr = 'SELECT * FROM '; protected $limit = ''; protected $order = ''; protected $config = array ( 'HOST' => 'localhost', 'PORT' => 3306, 'CHARSET' => 'utf8', 'DB_NAME' => '', 'DB_USER' => 'root', 'DB_PWD' => '', 'TABLE' => '' ); public function __construct($config = array()) { if (! class_exists ( 'PDO' )) { throw new Exception ( '没有找到PDO这个类,请配置相关项!' ); } $this->config = array_merge ( $this->config, $config ); $this->table = $this->config ['TABLE']; try { $this->pdo = new PDO ( 'mysql:host=' . $this->config ['HOST'] . ';dbname=' . $this->config ['DB_NAME'] . ';port=' . $this->config ['PORT'], $this->config ['DB_USER'], $this->config ['DB_PWD'], array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'set names ' . $this->config ['CHARSET'] ) ); $this->pdo->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false ); } catch ( PDOException $p ) { throw new Exception ( $p->getMessage () ); } } public function table($table) { $this->table = $table; return $this; } public function count($where = array()) { if (! empty ( $where )) { $this->where ( $where ); } $this->selectStr = 'SELECT COUNT(*) AS _count FROM '; $this->limit = ' LIMIT 1 '; $re = $this->find (); return $re [0] ['_count']; } public function has($where = array()) { if (! empty ( $where )) $this->where ( $where ); elseif (empty ( $this->whereStr )) return false; $this->sql = $this->selectStr . $this->table . ' WHERE ' . trim ( $this->whereStr, ' AND ' ) . ' LIMIT 1'; $row = $this->prExRo (); return $row ? true : false; } public function limit($start, $end = '') { $this->limit = ' LIMIT ' . (( int ) $start) . (empty ( $end ) ? '' : ',' . ( int ) $end); return $this; } public function order($order) { $this->order = ' ORDER BY ' . $order . ' '; return $this; } public function update($data) { $keys = array_keys ( $data ); $this->valuesArr = array_merge ( array_values ( $data ), $this->valuesArr ); $kstr = implode ( ',', array_map ( function ($v) { return '`' . $v . '` = ? '; }, $keys ) ); $this->sql = 'UPDATE ' . $this->table . ' SET ' . $kstr . ' WHERE ' . trim ( $this->whereStr, ' AND ' ); return $this->prExRo (); } public function delete($where = array()) { if (! empty ( $where )) { $this->where ( $where ); } if (empty ( $this->valuesArr )) return 0; $this->sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->whereStr; return $this->prExRo (); } public function insert($data) { $key = array_keys ( $data ); $this->valuesArr = array_values ( $data ); $kstr = implode ( ',', array_map ( function ($v) { return '`' . $v . '`'; }, $key ) ); $vstr = implode ( ',', array_fill ( 0, count ( $key ), '?' ) ); $vstr = ' VALUES ( ' . $vstr . ' ) '; $kstr = ' ( ' . $kstr . ' )'; $this->sql = 'INSERT INTO ' . $this->table . $kstr . $vstr; $this->prExRo (); return $this->pdo->lastInsertId (); } private function arrToStr($arr, $ks, &$arrV) { $kstr = ''; foreach ( $arr as $k => $v ) { $k = strtoupper ( $k ); $flip = array_flip ( $v ); if (is_int ( end ( $flip ) )) continue; $kstr .= '(' . implode ( ' ' . $k . ' ', array_map ( function ($c) { return (strrpos ( $c, '.' ) || strrpos ( $c, '(' )) ? ' ' . $c . '= ? ' : ' `' . $c . '` = ? '; }, $flip ) ) . ') ' . $ks . ' '; foreach ( $v as $vv ) { $arrV [] = $vv; } } return trim ( $kstr, ' ' . $ks . ' ' ) . ' AND '; } private function arrayW($arr) { $c = 0; if (is_array ( $arr )) { $c ++; foreach ( $arr as $v ) { $c += self::arrayW ( $v ); break; } } return $c; } public function where($where) { $this->whereStr = ''; $kstrt = ''; $arrV = array (); if (self::arrayW ( $where ) == 1) { foreach ( $where as $k => $v ) { $kstrt = (strrpos ( $k, '.' ) || strrpos ( $k, '(' )) ? ' ' . $k . '= ? ' : '( `' . $k . '` = ? )'; $arrV [] = $v; } } else { foreach ( $where as $ks => $vs ) { if (strtoupper ( $ks ) == 'LIKE') { foreach ( $vs as $lk => $lv ) { $kstrt .= '(' . $lk . ' LIKE ?' . ') AND '; $arrV [] = $lv; } continue; } elseif ($ks == '&') { $kstrt .= '(' . implode ( '=', $vs ) . ') AND '; continue; } $kstrt .= self::arrToStr ( self::arrayW ( $where ) == 2 ? $where : $vs, strtoupper ( $ks ), $arrV ); } } $this->whereStr .= $kstrt; $this->valuesArr = $arrV; return $this; } public function field($fields = array()) { $fieldArr = array (); foreach ( $fields as $k => $v ) { $fieldArr [] = (is_int ( $k ) ? '' : ((strrpos ( $k, '.' ) || strrpos ( $k, '(' )) ? $k . ' AS ' : '`' . $k . '` AS ')) . ((strrpos ( $v, '.' ) || strrpos ( $v, '(' )) ? $v : '`' . $v . '`'); } $fieldStr = implode ( ',', $fieldArr ); $this->selectStr = 'SELECT ' . (empty ( $fieldStr ) ? '*' : $fieldStr) . ' FROM '; return $this; } public function find() { $this->sql = $this->selectStr . $this->table . (empty ( $this->whereStr ) ? '' : (' WHERE ' . trim ( $this->whereStr, ' AND ' ))) . $this->order . $this->limit; $this->prExRo (); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $resultArr = $this->statement->fetchAll (); $this->selectStr = 'SELECT * FROM '; return $resultArr; } protected function prExRo() { $this->statement = $this->pdo->prepare ( $this->sql ); $this->statement->execute ( $this->valuesArr ); return $this->statement->rowCount (); } public function rawQuery($sql, $values = array()) { if (empty ( $values )) { return self::query ( $sql ); } $this->statement = $this->pdo->prepare ( $sql ); $this->statement->execute ( $values ); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $resultArr = $this->statement->fetchAll (); return empty ( $resultArr ) ? $this->statement->rowCount () : $resultArr; } public function query($sql) { $this->sql = $sql; $this->statement = $this->pdo->query ( $sql ); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $row = $this->statement->rowCount (); $arr = $this->statement->fetchAll (); return empty ( $arr ) ? $row : $arr; } public function quote($string, $type = PDO::PARAM_STR) { return $this->pdo->quote ( $string, $type ); } public function querySql() { $vas = array (); foreach ( $this->valuesArr as $v ) { $vas [] = $this->pdo->quote ( $v ); } return vsprintf ( str_replace ( '?', '%s', $this->sql ), $vas ); } public function preSqlStr() { return $this->sql; } }
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。