欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现Mysql简易操作类

程序员文章站 2022-10-11 12:07:33
自己封装的mysql简易操作类,已塞在ben框架中,基于pdo来写的,代码风格上有些无厘头。。。 mysql.class.php

自己封装的mysql简易操作类,已塞在ben框架中,基于pdo来写的,代码风格上有些无厘头。。。

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('set names utf8');
  }
  public function drop($table){
    $sql = 'drop table '.$table.';';
    $re = $this->query($sql);
    if($re){
      return true;
    }else{
      return false;
    }
  }
  public function insert($table,$name,$value=null){
    $sql = "insert into ".$table.'(';
    if($value == null){
    $arrname = array_keys($name);
    $arrvalue = array_values($name);
    }else{
    $arrname = explode('|', $name);
    $arrvalue = explode('|', $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."'".$arrvalue[$i]."'";
      }else{
        $sql = $sql."'".$arrvalue[$i]."',";
      }
    }
    $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."='".$conditionsvalue."';";
    }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].'='."'".$arrvalue[$i]."'";
        }else{
          $sql.=$arrname[$i].'='."'".$arrvalue[$i]."',";
        }
      }
      $sql.=';';
    }
    $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."='".$conditionsvalue."';";
    }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].'='."'".$arrvalue[$i]."'";
        }else{
          $sql.=$arrname[$i].'='."'".$arrvalue[$i]."' and ";
        }
      }
      $sql.=';';
    }
    $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."= '".$value."' where ".$conditionsname."='".$conditionsvalue."';";
    }else{
      $sql = "update ".$table." set ".$name."= '".$value."' where ";
      $arrname = array_keys($conditionsname);
      $arrvalue = array_values($conditionsname);
      for($i=0;$i<count($arrname);$i++){
        if($i==count($arrname)-1){
          $sql.=$arrname[$i].'='."'".$arrvalue[$i]."'";
        }else{
          $sql.=$arrname[$i].'='."'".$arrvalue[$i]."' and ";
        }
      }
      $sql.=';';
    }
    $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;
  }
}