时间:2021-07-01 10:21:17 帮助过:12人阅读
PDO封装类
<?php class DBDAP { public $host = "localhost"; public $uid = "root"; public $pwd = "root"; public $dbname = "mydb"; public function Query($sql,$type=1) { $dsn = "mysql:dbname=$this->dbname;host=$this->host"; $pdo = new PDO($dsn,"$this->uid","$this->pwd"); $result = $pdo->query($sql); if($type=="1") //参数为1时,返回查询结果 { return $result->fetchall(PDO::FETCH_ASSOC);//括号内的参数必须填写,不然取得的数据会发生重复现象。若不写,返回的数据有关联数据也有索引数据。 } else { $zeng = $pdo->prepare($sql); if($type=="2") //参数为2时,可以进行预处理语句 { return $zeng; }else { return $result; } } } public function StrQuery($sql,$type=1) //此方法用于对取出的数据(二维数组)进行拼接字符串处理 { $dsn = "mysql:dbname=$this->dbname;host=$this->host"; $pdo = new PDO($dsn,"$this->uid","$this->pwd"); $result = $pdo->query($sql); if($type=="1") { $arr = $result->fetchall(PDO::FETCH_ASSOC); $str = ""; foreach($arr as $v) { $str = $str.implode("^",$v)."|"; } $str = substr($str,0,strlen($str)-1); return $str; } else { $zeng = $pdo->prepare($sql); if($type=="2") { return $zeng; } else { return $result; } } } public function JsonQuery($sql,$type=1) //此方法用于ajax中用于返回为jason数据类型时使用 { $dsn = "mysql:dbname=$this->dbname;host=$this->host"; $pdo = new PDO($dsn,"$this->uid","$this->pwd"); $result = $pdo->query($sql); if($type=="1") { $arr = $result->fetchall(PDO::FETCH_ASSOC); return json_encode($arr); } else { $zeng = $pdo->prepare($sql); if($type=="2") { return $zeng; } else { return $result; } } } }
学习到目前,自己封装的db类和pdo类
标签:font logs style 拼接 new strlen oca etc 括号