PHP implements database operation Model class

墨辰丷
Release: 2023-03-28 14:50:02
Original
1877 people have browsed it

This article mainly introduces the simple database operation Model class implemented by PHP. It analyzes the definition and usage skills of PHP database operation model class in the form of examples, including basic addition, deletion, modification and query functions of the database. Friends in need can refer to the following

This database model class can realize the addition, deletion, modification and query of the database and simplify the database operation.

1. config.php code:

<?php
 define("HOSTNAME","127.0.0.1");
 define("USERNAME","root");
 define("PASSWORD","");
 define("DATANAME","class");
Copy after login

2. Usage code:

<?php
 /*
  作者:shyhero
  */
 require("./config.php");
 class Model{
  private $link;
  //构造函数,初始化数据库连接
  public function __construct(){
   $this -> link = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME) or die("数据库连接失败");
   mysqli_set_charset($this -> link,"utf8");
  }
  //查找 1.表名 2.条件 3.值 如果不添加条件或者值,就全部查询
  public function find($table="",$key="",$value=""){
   if(!$key||!$value){
    $sql = "select * from {$table}";
   }else{
    $sql = "select * from {$table} where {$key} = &#39;{$value}&#39;";
   }
   $res = mysqli_query($this -> link,$sql);
   $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
   mysqli_free_result($res);
   return $arr;
  }
  //增加 1.表名 2.需要插入的字段 3.值1
  public function ins($table="",$zd="name,score",$value=""){
   $arr = explode(",",$value);
   $str = "";
   foreach($arr as $k => $v){
    $str .= "&#39;".$v."&#39;".",";
   }
   $str = rtrim($str,",");
   $sql = "insert into {$table}({$zd})values({$str})";
   $res = mysqli_query($this -> link,$sql);
   return mysqli_insert_id($this -> link);
  }
  //修改 1.表名 2.修改字段 3.值 4.条件 5.值
  public function upd($table="",$key="",$value="",$key2="",$value2=""){
   $sql = "update {$table} set {$key}=&#39;{$value}&#39; where {$key2}=&#39;{$value2}&#39;";
   $res = mysqli_query($this -> link,$sql);
   return mysqli_affected_rows($this -> link);
  }
  //删除 1.表名 2.条件 3.值
  public function del($table="",$key="",$value=""){
   $sql = "delete from {$table} where {$key}=&#39;{$value}&#39;";
   $res = mysqli_query($this -> link,$sql);
   return mysqli_affected_rows($this -> link);
  }
  //析构函数
  public function __destruct(){
   if(isset($res))
    mysqli_free_result($res);
   mysqli_close($this -> link);
  }
 }
 $m = new Model();
 //var_dump($m -> find("stu","id"));
 var_dump($m -> ins("stu","name","zhu"));
 //var_dump($m -> upd("stu","name","dujianing","id","1"));
 //var_dump($m -> del("stu","name","li"));
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP method to implement clockwise printing of matrices and spiral matrices

PHP method to determine whether a binary tree is symmetrical

PHP method to delete all files in a directory using one line of code

The above is the detailed content of PHP implements database operation Model class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template