Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. class MysqlClient {
  2. protected $_database = ''; // 数据库名
  3. protected $_hostname = ''; // host
  4. protected $_hostport = ''; // 端口
  5. protected $_username = ''; // 数据库密码
  6. protected $_password = ''; // 数据库用户名
  7. protected $_pconnect = ''; // 常连接
  8. protected $_query = null; // 常连接
  9. protected $_connection = null; // 创建的连接
  10.  
  11. protected $_table = ''; // 表名
  12. public $_column = array();
  13.  
  14. /**
  15. * 构造函数,完成配置和数据连接
  16. * @param array $config
  17. * @throws Exception
  18. * @return
  19. */
  20. public function __construct(array $config = null) {
  21. if (null === $config) {
  22. return false;
  23. }
  24. $index = mt_rand(0, count($config['hostname']) -1);
  25.  
  26. $this->_database = $config['database'];
  27. $this->_hostname = $config['hostname'][$index];
  28. $this->_hostport = $config['hostport'];
  29. $this->_username = $config['username'];
  30. $this->_password = $config['password'];
  31. $this->_pconnect = $config['pconnect'];
  32. $this->connect();
  33. }
  34.  
  35. /**
  36. * 数据库连接
  37. * @throws Exception
  38. * @return unknown
  39. */
  40. public function connect() {
  41. $this->_start = microtime(true);
  42. $func = $this->_pconnect ? 'mysql_pconnect' : 'mysqli';
  43. $connection = new mysqli($this->_hostname, $this->_username, $this->_password, $this->_database, $this->_hostport);
  44. if ( $connection -> connect_error ) {
  45. return;
  46. }
  47. else {
  48. $this->_connection = $connection;
  49. $this->query('set names utf8;');
  50. }
  51. }
  52.  
  53. /**
  54. * @return
  55. */
  56. public function error() {
  57. return mysqli_error($this->_connection);
  58. }
  59.  
  60. /**
  61. * 数据库重连接
  62. * Enter description here ...
  63. * @return
  64. */
  65. public function reconnect() {
  66. if (false === @mysqli_ping($this->_connection)) {
  67. mysqli_close($this->_connection);
  68. return $this->connect();
  69. }
  70. return $this->_connection;
  71. }
  72.  
  73. /**
  74. * sql的增删改查
  75. * @param unknown_type $sql
  76. * @param unknown_type $type
  77. * @throws Exception
  78. * @return unknown
  79. */
  80. public function query($sql, $type = 'ASSOC') {
  81. if (false === ($this->_query = mysqli_query($this->_connection,$sql))) {
  82. $this->reconnect();
  83. if (false === ($this->_query = mysqli_query($this->_connection,$sql))) {
  84. $message = 'Invalid query: ' . $this->error();
  85. return -1;
  86. }
  87. }
  88. $tags = explode(' ', $sql, 2);
  89. switch (strtoupper($tags[0])) {
  90. case 'SELECT':
  91. ($result = $this->fetchAll($type)) || ($result = array());
  92. break;
  93. case 'INSERT':
  94. case 'REPLACE':
  95. $result = mysqli_insert_id($this->_connection);
  96. break;
  97. case 'UPDATE':
  98. case 'DELETE':
  99. $result = mysqli_affected_rows($this->_connection);
  100. break;
  101. default:
  102. $result = $this->_query;
  103. }
  104. return $result;
  105. }
  106.  
  107. /**
  108. * 获取select结果
  109. * @param unknown_type $type
  110. * @return Ambigous <multitype:, unknown>
  111. */
  112. public function fetchAll($type = 'ASSOC') {
  113. switch ($type) {
  114. case 'ASSOC':
  115. $func = 'mysqli_fetch_assoc';
  116. break;
  117. case 'NUM':
  118. $func = 'mysqli_fetch_array';
  119. break;
  120. case 'OBJECT':
  121. $func = 'mysqli_fetch_object';
  122. break;
  123. default:
  124. $func = 'mysqli_fetch_assoc';
  125. }
  126. $result = array();
  127. while (false != ($row = $func($this->_query))) {
  128. $result[] = $row;
  129. }
  130. @mysqli_free_result($this->_query);
  131. return $result;
  132. }
  133.  
  134. /**
  135. * @return
  136. * @param esc
  137. */
  138. public function real_escape_string($escapestr){
  139. return $this->_connection->real_escape_string($escapestr);
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement