<?php class PDO_Oper { private $hostname='127.0.0.1;'; private $username='root'; private $password='52ebook'; private $dbname='b_work'; private $conn; private static $fetch_mode; function __construct() { $this->conn=new PDO('mysql:host='.$this->hostname."dbname=".$this->dbname,$this->username,$this->password); //设置编码 $this->conn->exec("set names utf8"); } //查询整个数据集,管理字段数据 public function QueryAll($sql,$Fetch_mode) { $result=$this->conn->query($sql); $result->setFetchMode(self::FetchAction('num')); return $result->fetchAll(); } //执行删改操作 public function ExecSql($sql) { return $this->conn->Exec($sql); } //数据集模式 public static function FetchAction($fmode) { switch($fmode) { case "assoc"; self::$fetch_mode=PDO::FETCH_ASSOC; break; case "num"; self::$fetch_mode=PDO::FETCH_NUM; break; case "object"; self::$fetch_mode=PDO::FETCH_OBJ; break; case "both"; self::$fetch_mode=PDO::FETCH_BOTH; break; default: self::$fetch_mode=PDO::FETCH_ASSOC; break; } return self::$fetch_mode; } //防止sql注入 public function PreExecSql($sql,$par_array) { try{ $pare=$this->conn->prepare($sql); $pare->execute($par_array); } catch(Exception $exception) { echo $exception->getMessage(); } } //绑定参数,防止sql注入 public function PreParamExecSql($sql,$par_array) { try{ $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $pare=$this->conn->prepare($sql); for($n=0;$n<sizeof($par_array);$n++) { foreach($par_array[$n] as $key=>$value) { echo $key."=>".$value; $pare->bindValue($key,$value); } $pare->execute(); } } catch(PDOException $exception) { echo $exception->getMessage(); } } } ?>