pdo_helper数据库PDO操作类(未精简)第一部分
king老师,还有各位大大们,帮忙看下,我自己写的这个pdo操作类,不足的地方请指出。谢谢了!
<?php /**************** * pdo_dbhelper.class.php * pdo数据库操作帮助类 * ******************/ class Pdo_dbhelper{ private $conn;//PDO对象 private $stmt;//PDOStatement对象 public function __construct(){ $this->getConnection(); } public function getConnection(){ $database="anime"; $host="127.0.0.1"; $dns="mysql:dbname=".$database.";host=".$host; $user="root"; $pwd="mysql"; $charset="utf8"; try{ $pdo=new PDO($dns,$user,$pwd); $pdo->exec("set names ".$charset); $this->conn=$pdo; }catch(PDOException $e){ echo $e->getMessage(); } } public function preparedStatement($str_sql,$params){ try{ $this->stmt=$this->conn->prepare($str_sql); $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//设置报错模式 if(!empty($params)){ foreach($params as $key=>$value){ $this->stmt->bindParam($key+1,$value); } } $result=$this->stmt->execute(); return $result; }catch(PDOException $e){ $e->getMessage(); } } /* * @function 添加数据 * @param string $table 表名, array $params 列名和值组成的数组 * @return boolean 返回true表示添加数据成功,返回false表示添加失败 */ public function create($table,$params){ $str_sql="INSERT INTO `".$table."` ("; $columnsArr=array(); $placeholder=array(); $valuesArr=array(); foreach($params as $key=>$value){ $columnsArr[]="`".$key."`"; $placeholder[]="?"; if(is_numeric($value)){ $valuesArr[]=$value; } else{ $valuesArr[]="\'".$value."\'"; } } $str_columns=implode(",",$columnsArr); $str_values=implode(",",$placeholder); $str_sql.=$str_columns.") VALUES(".$str_values.")"; return $this->preparedStatement($str_sql,$valuesArr); } /* * @function 修改数据 * @param string $table 表名, array $params 列名和值组成的数组, array $condition 部位=>array("id","=","5")组成的条件数组 * @return boolean 返回true表示修改数据成功,返回false表示修改失败 */ public function update($table,$params,$condition){ $str_sql="UPDATE ".$table." SET "; $columnsArr=array(); $valuesArr=array(); foreach($params as $key=>$value){ $columnsArr[]="`".$key."`=?"; if(is_numeric($value)){ $valuesArr[]=$value; } else{ $valuesArr[]="\'".$value."\'"; } } $str_columns=implode(",",$columnsArr); $condition_arr=$this->analyzeCondition($condition);//分析条件,返回数组 $str_sql.=$str_columns.$condition_arr["str_condition"]; if(!empty($condition_arr["value_arr_condition"])){ $valuesArr=array_merge($valuesArr,$condition_arr["value_arr_condition"]); } return $this->preparedStatement($str_sql,$valuesArr); } /* * @function 删除数据(一般不做物理删除) * @param string $table 表名, array $condition 部位=>array("id","=","5")组成的条件数组 * @return boolean 返回true表示删除数据成功,返回false表示删除失败 */ public function delete($table,$condition){ $str_sql="DELETE FROM `".$table."` "; $valuesArr=array(); $condition_arr=$this->analyzeCondition($condition); $str_sql.=$condition_arr["str_condition"]; //肯定有where条件不然就删表了,所以编码的时候就避免 if(empty($condition_arr["value_arr_condition"])){ return false; } else{ $valuesArr=$condition_arr["value_arr_condition"]; } return $this->preparedStatement($str_sql,$valuesArr); } /* * @function 读取数据 * @param string $table 表名, array $params 由列名组成的一维数组 array $condition 部位=>array(array("id","=","5"),)组成的条件数组 * @return array */ public function readAll($table,$params,$condition){ $str_sql="SELECT "; $columnsArr=array(); $valuesArr=array(); foreach($params as $key=>$value){ $columnsArr[]="`".$value."`"; } $str_columns=implode(",",$columnsArr); $condition_arr=$this->analyzeCondition($condition); $str_sql.=$str_columns." FROM `".$table."`".$condition_arr["str_condition"]; echo $str_sql; if(!empty($condition_arr["value_arr_condition"])){ $valuesArr=$condition_arr["value_arr_condition"];//有条件的情况下, } $this->preparedStatement($str_sql,$valuesArr); return $this->stmt->fetchAll(PDO::FETCH_COLUMN,0); } /* * @function 返回受影响的行数 * @return string */ public function getfetchRowNum(){ if($this->stmt==null){ return false; } else{ return $this->stmt->rowCount(); } } /* * @function 返回最后插入行的ID * @return string */ public function getLastInsertId(){ if($this->conn==null){ return false; } else{ return $this->conn->lastInsertId(); } } /* * @function 分析condition数组 * @param $condition 条件数组 * @return array ["str_condition"]条件的字符串部分,["value_arr_condition"]条件的值组成的数组 */ public function analyzeCondition($condition){ $arr=array(); $arr["str_condition"]=""; $arr["value_arr_condition"]=array(); if(isset($condition["where"])){ $where_arr=$this->analyzeWhere($condition["where"]); $arr["str_condition"].=$where_arr["str"]; $arr["value_arr_condition"]=array_merge($arr["value_arr_condition"],$where_arr["value_arr"]); } if(isset($condition["group"])){ $group_arr=$this->analyzeGroup($condition["group"]); $arr["str_condition"].=$group_arr["str"]; $arr["value_arr_condition"]=array_merge($arr["value_arr_condition"],$group_arr["value_arr"]); } if(isset($condition["having"])){ $having_arr=$this->analyzeHaving($condition["having"]); $arr["str_condition"].=$having_arr["str"]; $arr["value_arr_condition"]=array_merge($arr["value_arr_condition"],$having_arr["value_arr"]); } if(isset($condition["order"])){ $order_arr=$this->analyzeOrder($condition["order"]); $arr["str_condition"].=$order_arr["str"]; $arr["value_arr_condition"]=array_merge($arr["value_arr_condition"],$order_arr["value_arr"]); } if(isset($condition["limit"])){ $limit_arr=$this->analyzeLimit($condition["limit"]); $arr["str_condition"].=$limit_arr["str"]; $arr["value_arr_condition"]=array_merge($arr["value_arr_condition"],$limit_arr["value_arr"]); } return $arr; } //接后续第二部分,太长了。