当前位置:   article > 正文

mysqli连接数据库实现链式增删改查类_mysqli连接数据库 更改字段值

mysqli连接数据库 更改字段值

 简单的用mysqli连接数据库实现的链式增删改查的类,大家可以自行扩展多表查询方法,关联查询方法等。对于用惯了thinkphp的同学来说,这个轻量级的类对于开发微型应用应该对您有所帮助。

  1. <?php
  2. define("HOST","host");
  3. define("USER", "user");
  4. define("PASSWORD", "pwd");
  5. define("DBNAME", "dbname");
  6. class Model{
  7. public $field = '*';
  8. public $tabname;
  9. public $where;
  10. public $order;
  11. public $limit;
  12. private $connect;
  13. //构造函数,链接数据库,给表赋值
  14. public function __construct($tabname){
  15. $this->connect = mysqli_connect(HOST,USER,PASSWORD,DBNAME);
  16. if (!$this->connect) die("连接错误: " . mysqli_connect_error());
  17. $this->tabname=$tabname;
  18. }
  19. //组合字段
  20. public function field($field){
  21. $this->field=$field;
  22. return $this;
  23. }
  24. //组合where条件
  25. public function where($where){
  26. $this->where="where ".$where;
  27. return $this;
  28. }
  29. //组合order排序条件
  30. public function order($order){
  31. $this->order="order by ".$order;
  32. return $this;
  33. }
  34. //组合limit限制条数
  35. public function limit($limit){
  36. $this->limit="limit ".$limit;
  37. return $this;
  38. }
  39. //组合和执行select语句
  40. public function select(){
  41. $sql="select {$this->field} from {$this->tabname} {$this->where} {$this->order} {$this->limit}";
  42. $rst=mysqli_query($this->connect,$sql);
  43. if(!$rst) $this->error();
  44. while ($row=mysqli_fetch_assoc($rst)) {
  45. $rows[]=$row;
  46. }
  47. return $rows;
  48. }
  49. //组合和执行insert语句单条插入
  50. public function insert($post){ //数据库插入操作,接收数组
  51. foreach ($post as $key => $value) {
  52. $keys[]=$key;
  53. $vals[]="'".$value."'";
  54. }
  55. $keyStr=join(",",$keys);
  56. $valStr=join(",",$vals);
  57. $sql="insert into {$this->tabname} ($keyStr) values ($valStr)";
  58. $rst = mysqli_query($this->connect,$sql);
  59. if ($rst) {
  60. return mysqli_insert_id($this->connect);
  61. }else{
  62. $this->error();
  63. }
  64. }
  65. //组合和执行insert语句多条插入
  66. /*
  67. $post['keys'] = ['id','name','old']
  68. $post['values'] = [
  69. [1,'张三','29'],
  70. [2,'李四','39']
  71. ]
  72. */
  73. public function inserts($post){ //数据库插入操作,接收二维数组
  74. $keyStr = join(",",$post['keys']);
  75. $valStr = '';
  76. foreach ($post['values'] as $value) {
  77. $vals = [];
  78. foreach ($value as $v){
  79. $vals[] = "'".$v."'";
  80. }
  81. $valStr .= '('.join(',',$vals).'),';
  82. }
  83. $valStr = trim($valStr,',');
  84. $sql="insert into {$this->tabname} ($keyStr) values $valStr";
  85. $rst = mysqli_query($this->connect,$sql);
  86. if ($rst) {
  87. return mysqli_affected_rows($this->connect);
  88. }else{
  89. $this->error();
  90. }
  91. }
  92. //删除数据
  93. public function delete(){
  94. if(!$this->where) die('删除数据必须指定删除条件,否则会删除数据库所有数据,请谨慎!');
  95. $sql="delete from {$this->tabname} {$this->where}";
  96. $rst = mysqli_query($this->connect,$sql);
  97. if ($rst) {
  98. return mysqli_affected_rows($this->connect);
  99. }else{
  100. $this->error();
  101. }
  102. }
  103. //组合和执行updata语句
  104. public function update($post){
  105. foreach ($post as $key => $value) {
  106. $sets[]="{$key}='{$value}'";
  107. }
  108. $setStr=join(",",$sets);
  109. $sql="update {$this->tabname} set {$setStr} {$this->where} ";
  110. $rst = mysqli_query($this->connect,$sql);
  111. if ($rst) {
  112. return mysqli_affected_rows($this->connect);
  113. }else{
  114. $this->error();
  115. }
  116. }
  117. //从表中取一行数据
  118. public function find(){
  119. $sql="select {$this->field} from {$this->tabname} {$this->where} limit 1";
  120. $rst=mysqli_query($this->connect,$sql);
  121. if(!$rst) $this->error();
  122. while ($row=mysqli_fetch_assoc($rst)) {
  123. $rows=$row;
  124. }
  125. return $rows;
  126. }
  127. //错误信息
  128. private function error(){
  129. die(mysqli_error($this->connect));
  130. }
  131. //获取总行数
  132. public function total(){
  133. $sql="select count(*) from {$this->tabname}";
  134. $rst=mysqli_query($this->connect,$sql);
  135. if ($rst) {
  136. $row=mysqli_fetch_row($rst);
  137. return $row[0];
  138. }else{
  139. $this->error();
  140. }
  141. }
  142. }
  143. //Model类对象工厂
  144. function M($tabname){
  145. return new Model($tabname);
  146. }

使用方法

  1. <?php
  2. include 'M.class.php';
  3. $data = [
  4. 'follow_group_id' => 1,
  5. 'follow_id' => 10,
  6. 'fid' => 20,
  7. 'uid' => 30
  8. ];
  9. $datas = [
  10. 'keys' => ['follow_group_id','follow_id','fid','uid'],
  11. 'values'=> [
  12. ['1',3,4,5],
  13. [6,7,8,9]
  14. ]
  15. ];
  16. //插入单条数据
  17. $insert = M('数据表名')->insert($data);
  18. //插入多条
  19. $inserts= M('数据表名')->inserts($datas);
  20. //查询多条数据
  21. $datas = M('数据表名')->field('id,name,old')->where('id>10')->order('id desc')->limit('0,50')->select();
  22. //查询单条数据
  23. $data = M('数据表名')->field('id,name,old')->where('id>10')->order('id desc')->limit('0,50')->find();
  24. //删除数据
  25. $del = M('数据表名')->where('id=10')->delete();
  26. //修改数据
  27. $update = M('数据表名')->where('id=10')->update($data);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/592878
推荐阅读
相关标签
  

闽ICP备14008679号