PHP implementation of simple database Operations model class _php skills

Source: Internet
Author: User

This article describes the PHP implementation of the simple database operation model class. Share to everyone for your reference, specific as follows:

The database model class can be used to check and modify database, and simplify database operation.

1. config.php Code:

<?php
 define ("HOSTNAME", "127.0.0.1");
 Define ("USERNAME", "root");
 Define ("PASSWORD", "");
 Define ("Dataname", "class");

2. Use code:

<?php/* Author: Shyhero * * require ("./config.php");
  Class model{private $link;  constructor, initializing the database connection public function __construct () {$this-> link = mysqli_connect (hostname,username,password,dataname)
   Or Die ("database connection failed");
  Mysqli_set_charset ($this-> link, "UTF8"); //Find 1. Table Name 2. Condition 3. Value if you do not add a condition or value, all queries public function find ($table = "", $key = "", $value = "") {if (! $key | |!
   $value) {$sql = "select * FROM {$table}";
   }else{$sql = "Select * FROM {$table} where {$key} = ' {$value} '";
   $res = mysqli_query ($this-> link, $sql);
   $arr = Mysqli_fetch_all ($res, MYSQLI_ASSOC);
   Mysqli_free_result ($res);
  return $arr;
   //Increase 1. Table Name 2. Fields that need to be inserted 3. value 1 public Function ins ($table = "", $zd = "Name,score", $value = "") {$arr = Explode (",", $value);
   $str = "";
   foreach ($arr as $k => $v) {$str. = "'. $v." ".", ";
   $str = RTrim ($str, ",");
   $sql = "INSERT INTO {$table} ({$zd}) VALUES ({$str})";
   $res = mysqli_query ($this-> link, $sql); return MySQLi_insert_id ($this-> link); //modify 1. Table Name 2. Modify Field 3. Value 4. Condition 5. Value Public Function upd ($table = "", $key = "", $value = "", $key 2= "", $value 2= "") {$sql = "updat
   e {$table} set {$key}= ' {$value} ' where {$key 2}= ' {$value 2} '];
   $res = mysqli_query ($this-> link, $sql);
  Return mysqli_affected_rows ($this-> link); //delete 1. Table Name 2. Condition 3. Value Public Function del ($table = "", $key = "", $value = "") {$sql = "delete from {$table} where {$key}= ' {
   $value} ' ";
   $res = mysqli_query ($this-> link, $sql);
  Return mysqli_affected_rows ($this-> link);
   }//destructor 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"));?>

More interested in PHP related content readers can view the site topics: "Php+mysql Database Operation Introduction Tutorial", "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "php+ Oracle Database Programming Skills Summary, "PHP+MSSQL Database Programming Skills Summary", "Php+redis Database Programming Skills Summary", "PHP+MYSQLI Database Programming Skills Summary" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.