<?php
/***数据操作类
* 对数据库的操作大多数是由$_POST进行的数组传参
* 所以增删改查默认传入的是$_POST 当新建一个对象时就直接写入要操作的数据表的tablename
* 1,对输入的$_POST数组进行处理 通过desc得到当前表的字段以及主键
* 2,增 insert 根据$_POST的传入字段 再修改 implode
* 3,删 delete 语句主要是where and or not = > < >= <=
* 4,改 update 神马字段改成神马样的 最主要的还是where条件
* 5,查 select 查找出神马字段 还就是where 利用add()进行where添加 以及 限制添加
* 6,order by ,group by ,desc 这些可以直接做 添加 and 当or的时候呢?
*/
class DB{
static $OrderCount=0;
static $LimitCount=0;
//表名
protected $TableName;
//主键
protected $Pk;
//提交的GET或者POST
protected $post;
//数据库连接资源
protected $Link=null;
//表的所有字段数组
protected $FieldsList=array();
//提交的字段数组
protected $PostKeyList=array();
//提交的字段的值组成的数组
protected $PostValueList=array();
//where 语句的形成
protected $where='';
//慢慢形成的sql语句
public $sql='';
/**
+----------------------------------------------------------
* 构造函数
+----------------------------------------------------------
* @access $TableName
+----------------------------------------------------------
* @param string $TableName
+----------------------------------------------------------
* @初始化链接
+----------------------------------------------------------
*/
public function __construct($TableName){
$this->TableName=$TableName;
$this->Link=@mysql_connect('localhost','root','')or die('Failed');
mysql_select_db('oop',$this->Link);
$this->post=$_REQUEST;
if($this->post){
$this->GetFields();
}
}
/**
+----------------------------------------------------------
* 获取字段函数
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param void
+----------------------------------------------------------
* @获取 $Pk $FieldsList $PostKeyList $PostValueList
+----------------------------------------------------------
*/
public function GetFields(){
//获取 所有字段 主键 以及提交的键和值
$sql="desc ".$this->TableName;
$res=mysql_query($sql);
while($rows=mysql_fetch_assoc($res)){
//获取该表的所有的字段
$this->FieldsList[]=$rows['Field'];
if($rows['Key']=="PRI"){
//获取该表的主键
$this->Pk=$rows['Field'];
}
}
//获取提交的键和值
foreach($this->post as $k=>$v){
if(in_array($k,$this->FieldsList)){
$this->PostKeyList[]=$k;
$this->PostValueList[]=$v;
}
}
}
/**
+----------------------------------------------------------
* order分析
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $order int $desc
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
public function order($order,$desc=0){
$order=explode(',',$order);
if(self::$OrderCount==0)
$this->sql.="order by ";
foreach($order as $v){
/* if(!in_array($v,$this->FieldsList)){
return;} */
self::$OrderCount++;
switch($desc){
case 0:
$z=1;
if($o==1)
$this->sql.=',';
$this->sql.=$v.',';
break;
case 1:
if($z==1)
$this->sql.=',';
$o=1;
$this->sql.=$v.' DESC';
break;
}
}
return $this;
}
/**
+----------------------------------------------------------
* limit分析
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param $start,$len 或者$len
+----------------------------------------------------------
*/
public function limit(){
$args=func_get_args();
if(count($args)==1){
$this->sql.=' limit '.$args[0];
}else{
$this->sql.=' limit '.$args[0].','.$args[1];
}
self::$LimitCount++;
return $this;
}
/**
+----------------------------------------------------------
* where分析
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param void
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
public function where(){
for($i=0;$this->PostKeyList[$i]!=null;$i++){
$str[]=$this->PostKeyList[$i]."='".$this->PostValueList[$i]."'";
}
switch($and){
case 0:
$a=' and ';
break;
case 1:
$a=' or ';
break;
}
$this->where=implode($a,$str);
return $this->where;
}
/**
+----------------------------------------------------------
* 插入记录
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param 需要插入的数组格式
+----------------------------------------------------------
*/
public function insert(){
$this->sql="insert into ".$this->TableName."(".implode(',',$this->PostKeyList).") values('".implode("','",$this->PostValueList)."')";
$this->query();
}
/**
+----------------------------------------------------------
* 删除记录
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param 需要删除的where 条件
+----------------------------------------------------------
*/
public function delete(){
$this->sql="delete from ".$this->TableName.' where '.$this->where();
$this->query();
}
/**
+----------------------------------------------------------
* 更新记录
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param 获得的更改数组也就是提交的数组
+----------------------------------------------------------
*/
public function update(){
//除去id的设置
foreach($this->PostKeyList as $k=>$v){
if($v!=$this->Pk){
$set[]=$v."='".$this->PostValueList[$k]."'";
}
}
$set=implode(',',$set);
$this->sql="update ".$this->TableName." set ".$set.' where '.$this->Pk."=".$this->post[$this->Pk];
$this->query();
}
/**
+----------------------------------------------------------
* 查找记录
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param and或者or 提交的where查找信息 以及order limit 信息
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function select($and='and'){
if($this->post==array()){
return;
}
for($i=0;$this->PostKeyList[$i]!=null;$i++){
$str[]=$this->PostKeyList[$i]."='".$this->PostValueList[$i]."'";
}
switch($and){
case 'and':
$a=' and ';
break;
case 'or':
$a=' or ';
break;
}
$str=implode($a,$str);
$this->sql.="select * from ".$this->TableName." where ".$str;
if(self::$OrderCount){
$this->order();
}
if(self::$LimitCount){
$this->limit();
}
}
/**
+----------------------------------------------------------
* 非select类执行sql
+----------------------------------------------------------
* @access protected
+----------------------------------------------------------
* @return 执行结果
+----------------------------------------------------------
*/
public function SelectQuery(){
$re=mysql_query($this->sql);
if($re or mysql_affected_rows()){
while($rows=mysql_fetch_assoc($re)){
var_dump($rows);
}
}else{
return FALSE;
}
}
protected function query(){
$re=mysql_query($this->sql);
if($re or mysql_affected_rows()){
return mysql_insert_id($this->Link);
}else{
return FALSE;
}
}
/**
+----------------------------------------------------------
* SQL输出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param void
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
public function ShowSql(){
echo $this->sql;
}
/**
+----------------------------------------------------------
* 析构方法
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function __destruct(){
if($this->Link){
mysql_close($this->Link);
}
}
}
define('DESC',1);
$d=new DB('user');
$d->select($_GET);
$d->order('classid')->order('age',DESC)->limit(1);
$d->SelectQuery();
$d->ShowSql();
?>
原文地址:http://bbs.lampbrother.net/read-htm-tid-121310.html