Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MysqlClient {
- protected $_database = ''; // 数据库名
- protected $_hostname = ''; // host
- protected $_hostport = ''; // 端口
- protected $_username = ''; // 数据库密码
- protected $_password = ''; // 数据库用户名
- protected $_pconnect = ''; // 常连接
- protected $_query = null; // 常连接
- protected $_connection = null; // 创建的连接
- protected $_table = ''; // 表名
- public $_column = array();
- /**
- * 构造函数,完成配置和数据连接
- * @param array $config
- * @throws Exception
- * @return
- */
- public function __construct(array $config = null) {
- if (null === $config) {
- return false;
- }
- $index = mt_rand(0, count($config['hostname']) -1);
- $this->_database = $config['database'];
- $this->_hostname = $config['hostname'][$index];
- $this->_hostport = $config['hostport'];
- $this->_username = $config['username'];
- $this->_password = $config['password'];
- $this->_pconnect = $config['pconnect'];
- $this->connect();
- }
- /**
- * 数据库连接
- * @throws Exception
- * @return unknown
- */
- public function connect() {
- $this->_start = microtime(true);
- $func = $this->_pconnect ? 'mysql_pconnect' : 'mysqli';
- $connection = new mysqli($this->_hostname, $this->_username, $this->_password, $this->_database, $this->_hostport);
- if ( $connection -> connect_error ) {
- return;
- }
- else {
- $this->_connection = $connection;
- $this->query('set names utf8;');
- }
- }
- /**
- * @return
- */
- public function error() {
- return mysqli_error($this->_connection);
- }
- /**
- * 数据库重连接
- * Enter description here ...
- * @return
- */
- public function reconnect() {
- if (false === @mysqli_ping($this->_connection)) {
- mysqli_close($this->_connection);
- return $this->connect();
- }
- return $this->_connection;
- }
- /**
- * sql的增删改查
- * @param unknown_type $sql
- * @param unknown_type $type
- * @throws Exception
- * @return unknown
- */
- public function query($sql, $type = 'ASSOC') {
- if (false === ($this->_query = mysqli_query($this->_connection,$sql))) {
- $this->reconnect();
- if (false === ($this->_query = mysqli_query($this->_connection,$sql))) {
- $message = 'Invalid query: ' . $this->error();
- return -1;
- }
- }
- $tags = explode(' ', $sql, 2);
- switch (strtoupper($tags[0])) {
- case 'SELECT':
- ($result = $this->fetchAll($type)) || ($result = array());
- break;
- case 'INSERT':
- case 'REPLACE':
- $result = mysqli_insert_id($this->_connection);
- break;
- case 'UPDATE':
- case 'DELETE':
- $result = mysqli_affected_rows($this->_connection);
- break;
- default:
- $result = $this->_query;
- }
- return $result;
- }
- /**
- * 获取select结果
- * @param unknown_type $type
- * @return Ambigous <multitype:, unknown>
- */
- public function fetchAll($type = 'ASSOC') {
- switch ($type) {
- case 'ASSOC':
- $func = 'mysqli_fetch_assoc';
- break;
- case 'NUM':
- $func = 'mysqli_fetch_array';
- break;
- case 'OBJECT':
- $func = 'mysqli_fetch_object';
- break;
- default:
- $func = 'mysqli_fetch_assoc';
- }
- $result = array();
- while (false != ($row = $func($this->_query))) {
- $result[] = $row;
- }
- @mysqli_free_result($this->_query);
- return $result;
- }
- /**
- * @return
- * @param esc
- */
- public function real_escape_string($escapestr){
- return $this->_connection->real_escape_string($escapestr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement