日期:2014-05-16  浏览次数:20487 次

自己写的个DB类

<?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($an