詳解php封裝Mysql操作類

來源:互聯網
上載者:User
這個PHP實現的mysql的操作類完整版已經使用過了,而這個簡化版是經過修改完整版後的簡化版,適用在一般的 PHP 應用中,功能上可以實現基本的增刪改查的操作,以及列印 MYSQL 錯誤,自我感覺不錯,如果網站應用程式不是很強大,應用這個 MYSQL 的操作類已經足夠了。希望對大家有所協助。

自己封裝的Mysql簡易操作類,已塞在Ben架構中,基於PDO來寫的,代碼風格上有些無厘頭。。。

mysql.class.php

<?phpclass 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;  }}

相關推薦:

PHP的session還原序列化漏洞詳解

詳解PHP計算學產生績的穩定性的方法

詳解PHP如何把資料庫匯出到csv檔案的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.