菜鸟一枚,希望得到大神们的指点。
<?php /* *---------------------------------------------------------------------------------------------------- *SELECT *$where=array('name'=>$name,'time'=>$time) *$db=new db('tablename');$result_array=$db->field($field)->where($where)->select() *$db=new db('tablename');$result_array=$db->field('field1,field2')->where('where1,where2')->select() *------------------------------------------------------------------------------------------------------ * *DELETE *$db=new db('tablename');$result=$db->where($where)->delete() *------------------------------------------------------------------------------------------------------ * *INSERT *$db=new db('tablename');$result=$db->data($data)->insert() *$db=new db('tablename');$result=$db->data('name=$name,nid=$nid')->insert() *------------------------------------------------------------------------------------------------------ * *UPDATE *$db=new db('tablename');$result=$db->data($data)->updata() *$db=new db('tablename');$result=$db->data('name=$name,nid=$nid')->updata() */ class db{ private $db_handle; private $db_handle2; private $tablename; private $where_str; private $data_str; private $field_str; private $query_sql; private $result; private $other_str; private $multi_db=false; /* * 构造函数,判断是否有主从库,连接数据库 */ function __construct($tablename){ $this->tablename=$tablename; } /* * 数据库连接 * @access private * @param $db_config * @return $handle */ private function connect($db_config){ $this->db_handle = mysql_connect($db_config['host'],$db_config['user'],$db_config['pwd']); if (!$this->db_handle){ return 0; }else{ if(mysql_select_db($db_config['db'],$this->db_handle)){ mysql_query("SET NAMES ".$db_config['charset']); return 1; }else{ return 0; } } } /* * 析构函数,释放内存,断开连接 * */ function __destruct(){ unset($this->data_str); unset($this->db_handle); unset($this->tablename); unset($this->where_str); unset($this->field_str); unset($this->query_sql); unset($this->other_str); if(mysql_close($this->db_handle)!=0){ return 1; }else{ return 0; } } /* * where * @param 字符串或数组 where['_logic']为逻辑,如 OR AND * 返回 key='value',key='value' */ public function where($where){ if(is_string($where)){ $this->where_str=trim($where); }else{ if(isset($where['_logic'])){ $logic=strtoupper($where['_logic']); unset($where['_logic']); }else{ $logic='AND';//默认逻辑 AND } foreach ($where as $key=>$value){ $this->where_str.=' '.$logic.' '; $this->where_str.=$key."='".mysql_real_escape_string($value)."'"; } $substr_num=strlen($logic)+2; $this->where_str=substr($this->where_str, $substr_num);//获取逻辑符号的长度,去除最后多加的逻辑符合 } return $this; } /* * field 获取查询的,或者要更新插入的字段值。 */ public function field($field){ if(is_string($field)){ $this->field_str=trim($field); }else{ for($i=0;$i<count($field);$i++){ $this->field_str.=','.$field[$i]; } $this->field_str=substr($this->field_str, 1); } return $this; } /* * data 返回如(1,2)或者(1,2),(3,4)格式的 */ public function data($data){ if(is_string($data)){//插入的数据是字符串,只能是值,没有field $this->data_str.=$data; }else{//插入的是数组 $data_dimension=0; if(!isset($this->field_str)){//先判断有没有设置field,没有先设置 foreach ($data as $key=>$value){ if(is_array($value)){//如果data是二维数组,即要插入很多数据 $data_dimension=2; foreach ($value as $key2=>$value2){ $this->field_str.=','.$key2; } break; }else{//如果是一维数组, $this->field_str.=','.$key; } } $this->field_str=substr($this->field_str, 1); } if(0==$data_dimension){//如果已经设置了field_str,没有判断维数,先判断维数。 foreach ($data as $key=>$value){ if(is_array($value)){ $data_dimension=2; }else{ $data_dimension=1; } break; } } if(2==$data_dimension){//如果是二维数组 foreach ($data as $key=>$value){ $this->data_str.=',('; foreach ($value as $key2=>$value2){ $this->data_str.="'".$value2."',"; } $this->data_str=substr($this->data_str,0,(strlen($this->data_str)-1 )); $this->data_str.=')'; } $this->data_str=substr($this->data_str, 1); } if (1==$data_dimension){//一维数组 $this->data_str.='( '; foreach ($data as $key=>$value){ $this->data_str.="'".$value."'".','; } $this->data_str=substr($this->data_str, 0,(strlen($this->data_str)-1 )); $this->data_str.=')'; } } return $this; } /* * insert 数据插入 * @return 插入id */ public function insert(){ $this->query_sql.='INSERT INTO '.$this->tablename; if(isset($this->field_str)){ $this->query_sql.=' ('.$this->field_str.') '; } $this->query_sql.=' VALUES '.$this->data_str; $this->execute(); if($this->result){ echo 'success'; }else{ echo 'field'; } } /* * updata */ public function update(){ if(!isset($this->field_str)){ $this->query_sql.='UPDATE '.$this->tablename.' SET '.$this->data_str.' WHERE '.$this->where_str; if(isset($this->other_str)){$this->query_sql.=$this->other_str;} }else { $this->query_sql.='UPDATE '.$this->tablename.' SET '; $field_replaced=str_replace(array('(',')',' '), '',$this->field_str ); $field_array=explode(',', $field_replaced); $data_replaced=str_replace(array('(',')',' '), '',$this->data_str ); $data_array=explode(',', $data_replaced); for($i=0;$i<count($data_array);$i++){ $this->query_sql.=$field_array[$i]."=".$data_array[$i].","; } $this->query_sql=substr($this->query_sql, 0,(strlen($this->query_sql)-1)); $this->query_sql.=' WHERE '.$this->where_str; if(isset($this->other_str)){$this->query_sql.=$this->other_str;} } $this->execute(); if($this->result){ echo 'success'; }else{ echo 'field'; } } /* * select */ public function select(){ if(!isset($this->field_str)){ $this->field_str='*'; } $this->query_sql='SELECT '.$this->field_str.' FROM '.$this->tablename; if(isset($this->where_str)){$this->query_sql.=' WHERE '.$this->where_str;} if(isset($this->other_str)){$this->query_sql.=$this->other_str;} $this->execute(); $result_select=array(); if(mysql_num_rows($this->result)>0){ while ($row=mysql_fetch_assoc($this->result)){ $result_select[]=$row; } } mysql_free_result($this->result); return $result_select; } public function delete(){ $this->query_sql.='DELETE FROM '.$this->tablename; if(isset($this->where_str)){$this->query_sql.=' WHERE '.$this->where_str;} if(isset($this->other_str)){$this->query_sql.=$this->other_str;} $this->execute(); if($this->result){ echo 'success'; }else{ echo 'field'; } } /* * execute 执行sql语句,有从库,select连从库 * @param $this->query_sql 需要执行的sql * @return */ public function execute(){ require("config.db.php"); if(isset($db_config2)){ $this->multi_db=true; } if($this->multi_db){ if(0==strpos($this->query_sql, 'SELECT')){ $this->connect($db_config2); }else{ $this->connect($db_config); } }else{ $this->connect($db_config); } echo $this->query_sql; $this->result=mysql_query($this->query_sql); } /* * */ public function limit($limit){ $this->other_str.=' LIMIT '.$limit; return $this; } public function page($page,$num='20'){ $limit_pian=$page*$num; $this->other_str.=' LIMIT '.$limit_pian.','.$num; return $this; } /* * $param eg. string $group='name,tpye'; */ public function groupby($groupby){ $this->other_str.=' GROUP BY'.$groupby; return $this; } /* * orderby * @param $order eg.:string id DESC;array $order=array('id'='DESC','name'='ASC') */ public function orderby($order){ if(is_string($order)){ $this->other_str.=' ORDER BY '.$order; }else{ $this->other_str.=' ORDER BY '; foreach ($order as $key=>$value){ $this->other_str.=$key.' '; if('DESC'==$value){ $this->other_str.='DESC,'; }else{ $this->other_str.='ASC,'; } $this->other_str=substr($this->other_str, 0,(strlen($this->other_str)-1)); } } return $this; } } ?>
例如sae中的设置如下,支持主从库
<?php $db_config["host"] = SAE_MYSQL_HOST_M.":".SAE_MYSQL_PORT; //服务器地址 $db_config["user"] = SAE_MYSQL_USER; //数据库用户名 $db_config["pwd"] = SAE_MYSQL_PASS; //数据库密码 $db_config["db"] = SAE_MYSQL_DB; //数据库名称 $db_config["charset"] = "utf8";//数据库编码 $db_config2["host"] = SAE_MYSQL_HOST_S.":".SAE_MYSQL_PORT; //服务器地址 $db_config2["user"] = SAE_MYSQL_USER; //数据库用户名 $db_config2["pwd"] = SAE_MYSQL_PASS; //数据库密码 $db_config2["db"] = SAE_MYSQL_DB; //数据库名称 $db_config2["charset"] = "utf8";//数据库编码 ?>