Home > php教程 > php手册 > body text

php实现Mysql简易操作类

WBOY
Release: 2016-06-06 19:41:16
Original
888 people have browsed it

这个PHP实现的mysql的操作类完整版已经使用过了,而这个简化版是经过修改完整版后的简化版,适用在一般的 PHP 应用中,功能上可以实现基本的增删改查的操作,以

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

mysql.class.php

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;$iquery($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;$iquery($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;$iquery($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;$iquery($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 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!