Home >Backend Development >PHP Tutorial >Detailed explanation of PHP encapsulation of Mysql operation class

Detailed explanation of PHP encapsulation of Mysql operation class

*文
*文Original
2018-01-05 18:01:322402browse

The full version of the mysql operation class implemented by PHP has already been used, and this simplified version is a simplified version after modifying the full version. It is suitable for general PHP applications and can realize basic additions, deletions, modifications and queries. I feel good about operating and printing MYSQL errors. If the website application is not very powerful, it is enough to apply this MYSQL operation class. I hope to be helpful.

The Mysql simple operation class encapsulated by myself has been stuffed into the Ben framework and is written based on PDO. The code style is somewhat nonsensical. . .

mysql.class.php

<?php
class mysql extends PDO{
  public $server;
  public $database;
  public $user;
  public $password;
  public $sql;
  public function __construct($server,$database,$user,$password,$port=3306){
    $this->server = $server;
    $this->database = $database;
    $this->user = $user;
    $this->password = $password;
    parent::__construct("mysql:host=$server;port=$port;dbname=$database",$user,$password);
    $this->query(&#39;SET NAMES utf8&#39;);
  }
  public function drop($table){
    $sql = &#39;DROP TABLE &#39;.$table.&#39;;&#39;;
    $re = $this->query($sql);
    if($re){
      return true;
    }else{
      return false;
    }
  }
  public function insert($table,$name,$value=null){
    $sql = "INSERT INTO ".$table.&#39;(&#39;;
    if($value == null){
    $arrname = array_keys($name);
    $arrvalue = array_values($name);
    }else{
    $arrname = explode(&#39;|&#39;, $name);
    $arrvalue = explode(&#39;|&#39;, $value);
    }
    for($i=0;$i<count($arrname);$i++){
      if($i==count($arrname)-1){
        $sql = $sql.$arrname[$i];
      }else{
        $sql = $sql.$arrname[$i].",";
      }
    }
    $sql = $sql.")VALUES(";
    for($i=0;$i<count($arrvalue);$i++){
      if($i==count($arrvalue)-1){
        $sql = $sql."&#39;".$arrvalue[$i]."&#39;";
      }else{
        $sql = $sql."&#39;".$arrvalue[$i]."&#39;,";
      }
    }
    $sql .=");";
    $re = $this->query($sql);
    if($re){
      return true;
    }else{
      return false;
    }
  }
  public function delete($table,$Conditionsname,$Conditionsvalue=null){
    if($Conditionsvalue!=null){
      $sql = "DELETE FROM ".$table." WHERE ".$Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
    }else{
      $sql = "DELETE FROM ".$table." WHERE ";
      $arrname = array_keys($Conditionsname);
      $arrvalue = array_values($Conditionsname);
      for($i=0;$i<count($arrname);$i++){
        if($i==count($arrname)-1){
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
        }else{
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;,";
        }
      }
      $sql.=&#39;;&#39;;
    }
    $re = $this->query($sql);
    if($re){
      return true;
    }else{
      return false;
    }
  }
  public function select($table,$name,$Conditionsname,$Conditionsvalue=null){
    if($Conditionsvalue!=null){
      $sql = "SELECT ".$name." FROM ".$table." WHERE ".$Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
    }else{
      $sql = "SELECT ".$name." FROM ".$table." WHERE ";
      $arrname = array_keys($Conditionsname);
      $arrvalue = array_values($Conditionsname);
      for($i=0;$i<count($arrname);$i++){
        if($i==count($arrname)-1){
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
        }else{
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39; and ";
        }
      }
      $sql.=&#39;;&#39;;
    }
    $re = $this->query($sql);
    $row = $re->fetch();
    return $row[$name];
  }
  public function update($table,$name,$value,$Conditionsname,$Conditionsvalue=null){
    if($Conditionsvalue!=null){
      $sql = "UPDATE ".$table." SET ".$name."= &#39;".$value."&#39; WHERE ".$Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
    }else{
      $sql = "UPDATE ".$table." SET ".$name."= &#39;".$value."&#39; WHERE ";
      $arrname = array_keys($Conditionsname);
      $arrvalue = array_values($Conditionsname);
      for($i=0;$i<count($arrname);$i++){
        if($i==count($arrname)-1){
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
        }else{
          $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39; and ";
        }
      }
      $sql.=&#39;;&#39;;
    }
    $re = $this->query($sql);
    if($re){
      return true;
    }else{
      return false;
    }
  }
  public function group($table,$name){
    $sql = "SELECT ".$name." FROM ".$table.";";
    $return = array();
    $re = $this->query($sql);
    while($row = $re->fetch(PDO::FETCH_ASSOC)){
      array_push($return,$row[$name]);
    }
    return $return;
  }
  public function fetchall($sql){
    $return = array();
    $re = $this->query($sql);
    while($row = $re->fetch(PDO::FETCH_ASSOC)){
      array_push($return,$row);
    }
    return $return;
  }
}

Related recommendations:

Detailed explanation of PHP’s session deserialization vulnerability

Detailed explanation of how PHP calculates the stability of student scores

Detailed explanation of how PHP exports the database to csv file method

The above is the detailed content of Detailed explanation of PHP encapsulation of Mysql operation class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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