代码来源:自己整理编写
说明:早期的时候自己写框架用的一个数据库驱动,纯粹的自己编写!虽然没有什么用处,但是很具有纪念和参考价值!
<?php
/**
* mysql驱动类
*/
class db{
private $host='';
private $user='';
private $password='';
private $DBname='';
private $tbname='';
private $option=array('field'=>'*','where'=>'','order'=>'','limit'=>'');
public function __construct($host,$user,$password,$DBname,$tbname){
$this->tbname=$tbname;
$this->connect($host,$user,$password,$DBname);
}
private function connect($host,$user,$password,$DBname){
$con=mysql_connect($host,$user,$password) or die('链接数据库失败');
mysql_select_db($DBname) or die('数据库不存在');
}
public function where($condition=null){
if(!is_null($condition)){
$str='';
foreach ($condition as $key => $value){
if(is_array($value)){
$value[0]=strtolower($value[0]);
switch ($value[0]) {
case 'gt':
$str.=$key.'>\''.$value[1].'\' and ';
break;
case 'lt':
$str.=$key.'<\''.$value[1].'\' and ';
break;
case 'eq':
$str.=$key.'=\''.$value[1].'\' and ';
break;
case 'neq':
$str.=$key.'!=\''.$value[1].'\' and ';
break;
case 'egt':
$str.=$key.'>=\''.$value[1].'\' and ';
break;
case 'elt':
$str.=$key.'<=\''.$value[1].'\' and ';
break;
case 'like':
$str.=$key.' like\''.$value[1].'\' and ';
break;
}
}else{
$str.=$key.'=\''.$value.'\' and ';
}
}
$str=rtrim($str,' and');
$this->option['where']='WHERE '.$str;
}
return $this;
}
public function order($order,$type=''){
$this->option['order']="ORDER BY {$order} {$type}";
return $this;
}
public function limit($limit,$length){
$this->option['limit']="LIMIT {$limit},{$length}";
return $this;
}
public function field($field){
$this->option['field']=$field;
return $this;
}
public function select(){
//select id,name from user where id>2 and name like '%n%' order by id desc limit 0,20
$sql="SELECT {$this->option['field']} FROM {$this->tbname} {$this->option['where']} {$this->option['order']} {$this->option['limit']}";
$res=$this->query_sql(2,$sql);
return $res;
}
public function insert($condition){
$Nvalue='';
$Nkey='';
if(!$condition){
die('请输入条件');
}
foreach ($condition as $key => $value) {
$Nkey.=$key.',';
$Nvalue.='\''.$value.'\',';
}
$Nkey=rtrim($Nkey,',');
$Nvalue=rtrim($Nvalue,',');
$sql="INSERT INTO{$this->tbname}({$Nkey}) VALUES({$Nvalue})";
$res=$this->query_sql(1,$sql);
return $res;
}
public function update($condition){
$Nvalue='';
$Nkey='';
if(!$condition){
die('请输入条件');
}
foreach ($condition as $key => $value) {
$Nkey.=$key.',';
$Nvalue.='\''.$value.'\',';
}
$str='';
foreach ($setvalue as $key => $value) {
$str.=$key.'=\''.$value.'\',';
}
$Nstr=rtrim($str,',');
$Nkey=rtrim($Nkey,',');
$Nvalue=rtrim($Nvalue,',');
$sql="UPDATE {$tbname} SET {$Nstr} WHERE {$Nkey}={$Nvalue}";
$res=$this->query_sql(3,$sql);
return $res;
}
public function delete(){
$Nvalue='';
$Nkey='';
if(!$condition){
die('请输入条件');
}
foreach ($condition as $key => $value) {
$Nkey.=$key.',';
$Nvalue.='\''.$value.'\',';
}
$Nkey=rtrim($Nkey,',');
$Nvalue=rtrim($Nvalue,',');
$sql="DELETE FROM {$tbname} WHERE {$Nkey}={$Nvalue}";
$res=$this->query_sql(4,$sql);
return $res;
}
public function query($string){
$res=$this->query_sql($string);
return $res;
}
public function query_sql($type,$sql){
if(is_string($type)){
$sql=$type;
}
//die($sql);
$res=mysql_query($sql) or die(mysql_error());
//$res=mysql_query($sql);
if($type==2||is_string($type)){
$result=array();
while ( $array=mysql_fetch_assoc($res) ) {
$result[]=$array;
}
}else{
if($type==5){
$result=mysql_num_rows($res);
}elseif($type==1){
$result=mysql_insert_id();
}else{
$result=mysql_affected_rows();
if($result==-1){
$result=0;
}
}
}
return $result;
}
public function __call($name,$value){
die ("您使用的{$name}()方法不存在,请检查后再试!");
}
public function __destruct(){
mysql_close();
}
}
$db=new db('127.0.0.1','root','','lamp90shop','adminusers');
$db->field('id,username')->where(array('id'=>array('like',"%s%")))->limit(0,20)->order('id','desc')->select();
本文由 陌上花开 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jul 1, 2016 at 06:30 am