[Mysql数据库] php连mysql的工具类



  1. <?php
      1. <?php
        http://www.kmnk01.com/hxpfk/2015/jq_1124/96.html
      2. class mysql {
      3. private $defaultDB = null;
      4. private $link = null;
      5. private $sql = null;
      6. private $bindValue = null;
      7. public $num_rows = 0;
        http://www.kmnk01.com/hxpfk/2015/jq_1124/97.html
      8. public $affected_rows = 0;
      9. public $insert_id = 0;
      10. public $queries = 0;
      11. public function __construct() {
      12. if(func_num_args()) {
      13. $argv = func_get_arg(0);
      14. if(!empty($argv) && is_array($argv)) {
      15. $this->connect($argv);
        http://www.kmnk01.com/hxpfk/2015/jc_1124/98.html
      16. $argv['charset'] = isset($argv['charset']) ? $argv['charset'] : 'utf8';
      17. $this->setCharset($argv['charset']);
      18. }
      19. }
      20. }
      21. public function connect($argv, $charset = null) {
      22. if($this->link) return false;
      23. $argv = func_get_arg(0);
      24. $argv['port'] = isset($argv['port']) ? $argv['port'] : 3306;
      25. $this->link = mysqli_connect( $argv['host'], $argv['user'], $argv['password'], $argv['database'], $argv['port']);
      26. if(mysqli_connect_errno()) { echo mysqli_connect_error(); exit(0); }
      27. $this->defaultDB = $argv['database'];
      28. if($charset)
      29. $this->setCharset($charset);
      30. }
        http://www.kmnk01.com/hxpfk/2015/hzj_1124/99.html
      31. public function selectDB($database){
      32. $int = mysqli_select_db($this->link, $database);
      33. if($int) $this->defaultDB = $database;
      34. return $int;
      35. }
      36. public function query($sql) {
      37. $result = mysqli_query($this->link, $sql);
      38. if(mysqli_errno($this->link)) { echo mysqli_error($this->link); exit(0); }
      39. $this->queries++;
      40. if(preg_match('/^use\\s+(\\w+)/', $sql, $matches)) list($range, $this->defaultDB) = $matches;
      41. $pattern = array('read'=> '/(?<=select|show)(.+)$/i', 'write'=> '/(?<=alter|use|replace|insert|update|delete)(.+)$/i');
      42. if(preg_match($pattern['write'], $sql)) {
      43. $this->affected_rows = mysqli_affected_rows($this->link);
      44. }else{
      45. $this->num_rows = mysqli_num_rows($result);
      46. }
      47. http://www.kmnk01.com/hxpfk/2015/jq_1124/100.html
      48. if(preg_match('/^insert(.+)$/i', $sql)) {
      49. $this->insert_id = mysqli_insert_id($this->link);
      50. }
      51. return $result;
      52. }
      53. public function find($sql) {
      54. $collection = array();
      55. $result = $this->query($sql);
      56. while($rows = mysqli_fetch_assoc($result))
      57. array_push($collection, $rows);
      58. mysqli_free_result($result);
      59. return $collection;
      60. }
      61. public function findOne($sql) {
      62. $result = $this->query($sql);
      63. $rows = mysqli_fetch_assoc($result);
      64. mysqli_free_result($result);
      65. return $rows;
      66. }
      67. http://www.kmnk01.com/hxpfk/2015/bt_1123/92.html
      68. public function setCharset($charset) {
      69. return mysqli_set_charset($this->link, $charset);
      70. }
      71. public function prepare($sql) {
      72. $this->sql = $sql;
      73. }
      74. public function bindValue($search, $value) {
      75. $this->bindValue = array();
      76. $this->bindValue[$search] = $value;
      77. }
      78. public function execute() {
      79. if(func_num_args()) {
      80. $argv = func_get_arg(0);
      81. if(!empty($argv) && is_array($argv)) {
      82. if(!is_array($this->bindValue)) $this->bindValue = array();
      83. $this->bindValue = array_merge($this->bindValue, $argv);
      84. }
      85. }
      86. if($this->bindValue) {
      87. foreach($this->bindValue as $search => $value) {
      88. $this->sql = str_replace($search, $this->escape($value), $this->sql);
      89. }
      90. $this->bindValue = null;
      91. }
      92. $int = $this->query($this->sql);
      93. //$this->sql = null;
      94. return (boolean) $int;
      95. }
      96. public function escape($string) {
      97. return mysqli_real_escape_string($this->link, $string);
      98. }
      99. public function close() {
      100. http://www.kmnk01.com/hxpfk/2015/jq_1124/102.html
      101. return mysqli_close($this->link);
      102. }
      103. public function ping() {
      104. return mysqli_ping($this->link);
      105. }
      106. public function autoCommit($boolean) {
      107. return mysqli_autocommit($this->link, $boolean);
      108. }
      109. public function commit() {
      110. return mysqli_commit($this->link);
      111. }
      112. public function rollback() {
      113. return mysqli_rollback($this->link);
      114. }
      115. public function __destruct() {
      116. if($this->link) $this->close();
      117. unset($this->link, $this->defaultDB, $this->bindValue, $this->sql, $this->result, $this->num_rows, $this->affected_rows, $this->insert_id);
      118. }
      119. }
      120. $argv = array(
      121. 'host' => 'localhost',
      122. 'user' => 'root',
      123. 'password' => '',
      124. 'port' => 3306,
      125. 'database' => 'test',
      126. 'charset'=> 'utf8');
      127. // Using the "mysql::__construct" method to connect MySQL database
      128. $mysql = new mysql($argv);
      129. var_dump($mysql->find('select version()'));
      130. var_dump($mysql->queries);
      131. // Using the "mysql::connect" method to connect MySQL database
      132. $mysql = new mysql();
      133. $mysql->connect($argv);
      134. var_dump($mysql->find('select version()'));
      135. var_dump($mysql->queries);
      136. $mysql = new mysql();
      137. $mysql->connect($argv);
      138. $mysql->setCharset($argv['charset']);
      139. var_dump($mysql->find('select v
        http://www.kmnk01.com/hxpfk/2015/py_1124/103.html
        ersion()'));
      140. var_dump($mysql->queries);

      http://www.kmnk01.com/hxpfk/2015/jq_1124/96.html
      <?php
    1. class mysql {
    2. private $defaultDB = null;
    3. private $link = null;
    4. private $sql = null;
    5. private $bindValue = null;
    6. public $num_rows = 0;
    7. public $affected_rows = 0;
    8. public $insert_id = 0;
    9. public $queries = 0;
    10. public function __construct() {
    11. if(func_num_args()) {
    12. $argv = func_get_arg(0);
    13. if(!empty($argv) && is_array($argv)) {
    14. $this->connect($argv);
    15. $argv['charset'] = isset($argv['charset']) ? $argv['charset'] : 'utf8';
    16. $this->setCharset($argv['charset']);
    17. }
    18. }
    19. }
    20. public function connect($argv, $charset = null) {
    21. if($this->link) return false;
    22. $argv = func_get_arg(0);
    23. $argv['port'] = isset($argv['port']) ? $argv['port'] : 3306;
    24. $this->link = mysqli_connect( $argv['host'], $argv['user'], $argv['password'], $argv['database'], $argv['port']);
    25. if(mysqli_connect_errno()) { echo mysqli_connect_error(); exit(0); }
    26. $this->defaultDB = $argv['database'];
    27. if($charset)
    28. $this->setCharset($charset);
    29. }
    30. public function selectDB($database){
    31. $int = mysqli_select_db($this->link, $database);
    32. if($int) $this->defaultDB = $database;
    33. return $int;
    34. }
    35. public function query($sql) {
    36. $result = mysqli_query($this->link, $sql);
    37. if(mysqli_errno($this->link)) { echo mysqli_error($this->link); exit(0); }
    38. $this->queries++;
    39. if(preg_match('/^use\\s+(\\w+)/', $sql, $matches)) list($range, $this->defaultDB) = $matches;
    40. $pattern = array('read'=> '/(?<=select|show)(.+)$/i', 'write'=> '/(?<=alter|use|replace|insert|update|delete)(.+)$/i');
    41. if(preg_match($pattern['write'], $sql)) {
    42. $this->affected_rows = mysqli_affected_rows($this->link);
    43. }else{
    44. $this->num_rows = mysqli_num_rows($result);
    45. }
    46. if(preg_match('/^insert(.+)$/i', $sql)) {
    47. $this->insert_id = mysqli_insert_id($this->link);
    48. }
    49. return $result;
    50. }
    51. public function find($sql) {
    52. $collection = array();
    53. $result = $this->query($sql);
    54. while($rows = mysqli_fetch_assoc($result))
    55. array_push($collection, $rows);
    56. mysqli_free_result($result);
    57. return $collection;
    58. }
    59. public function findOne($sql) {
    60. $result = $this->query($sql);
    61. $rows = mysqli_fetch_assoc($result);
    62. mysqli_free_result($result);
    63. return $rows;
    64. }
    65. public function setCharset($charset) {
    66. return mysqli_set_charset($this->link, $charset);
    67. }
    68. http://www.kmnk01.com/hxpfk/2015/hzj_1124/99.html
    69. public function prepare($sql) {
    70. $this->sql = $sql;
    71. }
    72. public function bindValue($search, $value) {
    73. $this->bindValue = array();
    74. $this->bindValue[$search] = $value;
    75. }
    76. public function execute() {
    77. if(func_num_args()) {
    78. $argv = func_get_arg(0);
    79. if(!empty($argv) && is_array($argv)) {
    80. if(!is_array($this->bindValue)) $this->bindValue = array();
    81. $this->bindValue = array_merge($this->bindValue, $argv);
    82. }
    83. }
    84. if($this->bindValue) {
    85. foreach($this->bindValue as $search => $value) {
    86. $this->sql = str_replace($search, $this->escape($value), $this->sql);
    87. }
    88. $this->bindValue = null;
    89. }
    90. $int = $this->query($this->sql);
    91. //$this->sql = null;
    92. return (boolean) $int;
    93. }
    94. public function escape($string) {
    95. return mysqli_real_escape_string($this->link, $string);
    96. }
    97. public function close() {
    98. return mysqli_close($this->link);
    99. }
    100. public function ping() {
    101. return mysqli_ping($this->link);
    102. }
    103. public function autoCommit($boolean) {
    104. return mysqli_autocommit($this->link, $boolean);
    105. }
    106. public function commit() {
    107. return mysqli_commit($this->link);
    108. }
    109. public function rollback() {
    110. return mysqli_rollback($this->link);
    111. }
    112. public function __destruct() {
    113. if($this->link) $this->close();
    114. unset($this->link, $this->defaultDB, $this->bindValue, $this->sql, $this->result, $this->num_rows, $this->affected_rows, $this->insert_id);
    115. }
    116. }
    117. $argv = array(
    118. 'host' => 'localhost',
    119. 'user' => 'root',
    120. 'password' => '',
    121. 'port' => 3306,
    122. 'database' => 'test',
    123. 'charset'=> 'utf8');
    124. // Using the "mysql::__construct" method to connect MySQL database
    125. $mysql = new mysql($argv);
    126. var_dump($mysql->find('select version()'));
    127. var_dump($mysql->queries);
    128. // Using the "mysql::connect" method to connect MySQL database
    129. $mysql = new mysql();
    130. $mysql->connect($argv);
    131. var_dump($mysql->find('select version()'));
    132. var_dump($mysql->queries);
    133. $mysql = new mysql();
    134. $mysql->connect($argv);
    135. $mysql->setCharset($argv['charset']);
    136. var_dump($mysql->find('select version()'));
    137. var_dump($mysql->queries);

    http://www.kmnk01.com/hxpfk/2015/jq_1124/96.html
  2. class mysql {
  3. private $defaultDB = null;
  4. private $link = null;
  5. private $sql = null;
  6. private $bindValue = null;
  7. public $num_rows = 0;
  8. public $affected_rows = 0;
  9. public $insert_id = 0;
  10. public $queries = 0;
  11. public function __construct() {
  12. if(func_num_args()) {
  13. $argv = func_get_arg(0);
  14. if(!empty($argv) && is_array($argv)) {
  15. $this->connect($argv);
  16. $argv['charset'] = isset($argv['charset']) ? $argv['charset'] : 'utf8';
  17. $this->setCharset($argv['charset']);
  18. }
  19. }
  20. }
  21. public function connect($argv, $charset = null) {
  22. if($this->link) return false;
  23. $argv = func_get_arg(0);
  24. $argv['port'] = isset($argv['port']) ? $argv['port'] : 3306;
  25. $this->link = mysqli_connect( $argv['host'], $argv['user'], $argv['password'], $argv['database'], $argv['port']);
  26. if(mysqli_connect_errno()) { echo mysqli_connect_error(); exit(0); }
  27. $this->defaultDB = $argv['database'];
  28. if($charset)
  29. $this->setCharset($charset);
  30. }
  31. public function selectDB($database){
  32. $int = mysqli_select_db($this->link, $database);
  33. if($int) $this->defaultDB = $database;
  34. return $int;
  35. }
  36. public function query($sql) {
  37. $result = mysqli_query($this->link, $sql);
  38. if(mysqli_errno($this->link)) { echo mysqli_error($this->link); exit(0); }
  39. $this->queries++;
  40. if(preg_match('/^use\\s+(\\w+)/', $sql, $matches)) list($range, $this->defaultDB) = $matches;
  41. $pattern = array('read'=> '/(?<=select|show)(.+)$/i', 'write'=> '/(?<=alter|use|replace|insert|update|delete)(.+)$/i');
  42. if(preg_match($pattern['write'], $sql)) {
  43. $this->affected_rows = mysqli_affected_rows($this->link);
  44. }else{
  45. $this->num_rows = mysqli_num_rows($result);
  46. }
  47. if(preg_match('/^insert(.+)$/i', $sql)) {
  48. $this->insert_id = mysqli_insert_id($this->link);
  49. }
  50. return $result;
  51. }
  52. public function find($sql) {
  53. $collection = array();
  54. $result = $this->query($sql);
  55. while($rows = mysqli_fetch_assoc($result))
  56. array_push($collection, $rows);
  57. mysqli_free_result($result);
  58. return $collection;
  59. }
  60. public function findOne($sql) {
  61. $result = $this->query($sql);
  62. $rows = mysqli_fetch_assoc($result);
  63. mysqli_free_result($result);
  64. return $rows;
  65. }
  66. public function setCharset($charset) {
  67. return mysqli_set_charset($this->link, $charset);
  68. }
  69. public function prepare($sql) {
  70. $this->sql = $sql;
  71. }
  72. public function bindValue($search, $value) {
  73. $this->bindValue = array();
  74. $this->bindValue[$search] = $value;
  75. }
  76. public function execute() {
  77. if(func_num_args()) {
  78. $argv = func_get_arg(0);
  79. if(!empty($argv) && is_array($argv)) {
  80. if(!is_array($this->bindValue)) $this->bindValue = array();
  81. $this->bindValue = array_merge($this->bindValue, $argv);
  82. }
  83. }
  84. if($this->bindValue) {
  85. foreach($this->bindValue as $search => $value) {
  86. $this->sql = str_replace($search, $this->escape($value), $this->sql);
  87. }
  88. $this->bindValue = null;
  89. }
  90. $int = $this->query($this->sql);
  91. //$this->sql = null;
  92. return (boolean) $int;
  93. }
  94. public function escape($string) {
  95. return mysqli_real_escape_string($this->link, $string);
  96. }
  97. public function close() {
  98. return mysqli_close($this->link);
  99. }
  100. public function ping() {
  101. return mysqli_ping($this->link);
  102. }
  103. public function autoCommit($boolean) {
  104. return mysqli_autocommit($this->link, $boolean);
  105. }
  106. public function commit() {
  107. return mysqli_commit($this->link);
  108. }
  109. public function rollback() {
  110. return mysqli_rollback($this->link);
  111. }
  112. public function __destruct() {
  113. if($this->link) $this->close();
  114. unset($this->link, $this->defaultDB, $this->bindValue, $this->sql, $this->result, $this->num_rows, $this->affected_rows, $this->insert_id);
  115. }
  116. }
  117. $argv = array(
  118. 'host' => 'localhost',
  119. 'user' => 'root',
  120. 'password' => '',
  121. 'port' => 3306,
  122. 'database' => 'test',
  123. 'charset'=> 'utf8');
  124. // Using the "mysql::__construct" method to connect MySQL database
  125. $mysql = new mysql($argv);
  126. var_dump($mysql->find('select version()'));
  127. var_dump($mysql->queries);
  128. // Using the "mysql::connect" method to connect MySQL database
  129. $mysql = new mysql();
  130. $mysql->connect($argv);
  131. var_dump($mysql->find('select version()'));
  132. var_dump($mysql->queries);
  133. $mysql = new mysql();
  134. $mysql->connect($argv);
  135. $mysql->setCharset($argv['charset']);
  136. var_dump($mysql->find('select version()'));
  137. var_dump($mysql->queries);
 <?php class mysql { private $defaultDB = null; private $link = null; private $sql = null; private $
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值