- UID
- 1
- 贡献
- 844
- 金币
- 1540
- 主题
- 520
- 在线时间
- 333 小时
- 注册时间
- 2022-1-15
- 最后登录
- 2024-11-12
|
发表于 2022-12-10 21:07:09
| 331 |
0 |
显示全部楼层
|阅读模式
php教程数据查询部分
语法代码:
- <?php
- class MyPDO{
- ...
-
- //判断匹配的类型
- private function fetchType($type){
- switch ($type){
- case 'num':
- return PDO::FETCH_NUM;
- case 'both':
- return PDO::FETCH_BOTH;
- case 'obj':
- return PDO::FETCH_OBJ;
- default:
- return PDO::FETCH_ASSOC;
- }
- }
- //获取所有数据 ,返回二维数组
- public function fetchAll($sql,$type='assoc'){
- try{
- $stmt=$this->pdo->query($sql); //获取PDOStatement对象
- $type= $this->fetchType($type); //获取匹配方法
- return $stmt->fetchAll($type);
- } catch (Exception $ex) {
- $this->showException($ex, $sql);
- }
- }
- //获取一维数组
- public function fetchRow($sql,$type='assoc'){
- try{
- $stmt=$this->pdo->query($sql); //获取PDOStatement对象
- $type= $this->fetchType($type); //获取匹配方法
- return $stmt->fetch($type);
- } catch (Exception $ex) {
- $this->showException($ex, $sql);
- exit;
- }
- }
- //返回一行一列
- public function fetchColumn($sql){
- try{
- $stmt=$this->pdo->query($sql);
- return $stmt->fetchColumn();
- } catch (Exception $ex) {
- $this->showException($ex, $sql);
- exit;
- }
-
- }
-
- }
- //测试
- $param=array(
-
- );
- $mypdo= MyPDO::getInstance($param);
- //echo $mypdo->exec('delete from news where id=6');
- /*
- if($mypdo->exec("insert into news values (null,'11','1111',unix_timestamp())"))
- echo '自动增长的编号是:'.$mypdo->lastInsertId ();
- */
- //$list=$mypdo->fetchAll('select * from news');
- //$list=$mypdo->fetchRow('select * from news where id=1');
- $list=$mypdo->fetchColumn('select count(*) from news');
- echo '<pre>';
- var_dump($list);
复制代码
|
|