当前位置:Gxlcms > PHP教程 > PHPmysqli数据库操作类

PHPmysqli数据库操作类

时间:2021-07-01 10:21:17 帮助过:7人阅读

  1. <!--?php
  2. class Mysql{
  3. private $host = 'localhost';
  4. private $port = '3306';
  5. private $user = 'username';
  6. private $pwd = 'password';
  7. private $db = 'dbname';
  8. private $char = 'UTF8';
  9. private $prefix = '';
  10. private $fetch_mode = MYSQLI_ASSOC;//获取模式
  11. private $result;//结果集
  12. public $mysqli;//mysqli实例<strong-->对象
  13. static private $_instance;//本类实例
  14. //构造函数初始化$mysqli<strong>对象</strong>
  15. private function __construct() {
  16. $this->mysqli=new mysqli($this->host,$this->user,$this->pwd,$this->db,$this->port);
  17. if(mysqli_connect_errno()){
  18. $this->mysqli=false;
  19. echo mysqli_connect_error();
  20. die();
  21. }else{
  22. $this->mysqli->set_charset($char);
  23. }
  24. }
  25. //析构函数:释放结果集和关闭数据库
  26. public function __destruct(){
  27. $this->free();
  28. $this->close();
  29. }
  30. //初始化$mysqli<strong>对象</strong>
  31. public static function getInstance(){
  32. if(!(self::$_instance instanceof self)) {
  33. self::$_instance = new self();
  34. }
  35. return self::$_instance;
  36. }
  37. //释放结果集
  38. private function free(){
  39. @$this->result->free();
  40. }
  41. //关闭数据库连接
  42. private function close(){
  43. $this->mysqli->close();
  44. }
  45. //执行sql语句
  46. public function query($sql){
  47. return $this->mysqli->query($sql);
  48. }
  49. //获取查询结果
  50. public function get_result_array($table,$field,$c
  51. $table=$this->prefix.$table;
  52. if(is_array($field)){
  53. $field = join(',',$field);
  54. }
  55. $sql = "SELECT $field FROM ".$table;
  56. if(!empty($condition))$sql .=" $condition ";
  57. $this->result = $this->query($sql);
  58. $return = array();
  59. while($row = $this->fetch($this->result)){
  60. $return[] = $row;
  61. }
  62. $this->free();
  63. return $return;
  64. }
  65. //增删改操作
  66. public function execute($table,$action,$arr_field=array(),$c
  67. $table=$this->prefix.$table;
  68. switch($action){
  69. case 'INSERT':
  70. $str_field = '';
  71. $str_val = '';
  72. foreach($arr_field as $key=>$val){
  73. $str_field .= '`'.$key.'`,';
  74. $str_val .= '\''.$val.'\',';
  75. }
  76. $str_field = rtrim($str_field, ',');
  77. $str_val = rtrim($str_val, ',');
  78. $sql = "INSERT INTO $table ($str_field) VALUES ($str_val) ";
  79. break;
  80. case 'DELETE':
  81. $sql = "DELETE FROM $table";
  82. if (!empty($condition)) $sql .= " WHERE $condition";
  83. break;
  84. case 'UPDATE':
  85. $str_field = '';
  86. foreach($arr_field as $key => $val){
  87. $str_field.= '`'.$key ."` ='$val',";
  88. }
  89. $str_field = rtrim($str_field, ',');
  90. $sql = "UPDATE $table SET $str_field";
  91. if (!empty($condition)) $sql .= " WHERE $condition";
  92. break;
  93. }
  94. $this->query($sql);
  95. return $this->get_affected_rows();
  96. }
  97. //获得受影响行数(针对增删改操作)
  98. public function get_affected_rows(){
  99. return $this->mysqli->affected_rows;
  100. }
  101. //获取集合条数
  102. public function get_rows($table,$c 1 ',$id='id'){
  103. $table=$this->prefix.$table;
  104. $sql="SELECT COUNT($id) num FROM ".$table." $condition";
  105. $this->result=$this->query($sql);
  106. $row=$this->fetch($this->result);
  107. return $row['num'];
  108. }
  109. //获得结果集
  110. public function fetch($result){
  111. return $result->fetch_array($this->fetch_mode);
  112. }
  113. //获得所有结果集
  114. public function fetch_all($result){
  115. $rows=array();
  116. while($row=$this->fetch($result)){
  117. $rows[]=$row;
  118. }
  119. return $rows;
  120. }
  121. }

想用PHP写手机App的接口封装的一个数据库操作类

以上就介绍了PHP mysqli数据库操作类,包括了对象方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行