您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

解析PHP中的PDO::fetch()方法

2024/4/7 5:41:09发布17次查看
php中经常需要对数据库中的数据进行查询,pdo是现下较为主流的连接方式,pdo::fetch()是pdo中查询数据的主要方法,本文就带大家一起来看一看如何使用fetch()方法。
首先我们来看一下fetch()方法的语法:
fetch ( int $fetch_style = ? , int $cursor_orientation = pdo::fetch_ori_next , int $cursor_offset = 0)
$fetch_style:控制下一行如何返回给调用者
$ursor_orientation:对于 一个 pdostatement 对象表示的可滚动游标,该值决定了哪一行将被返回给调用者。
$offset:对于一个 $cursor_orientation 参数设置,若为pdo::fetch_ori_rel,获取行相对于调用 pdostatement::fetch() 前游标的位置;若为pdo::fetch_ori_abs,指定结果集中想要获取行的绝对行号。
此方法成功时返回的值依赖于提取类型。在所有情况下,失败都返回 false.
代码示例
1.连接数据库
<?php$servername="localhost";$username="root";$password="root123456";$dbname="my_database";$pdo=new pdo("mysql:host=$servername;dbname=$dbname",$username,$password);echo "连接成功"."<br>";$pdo->setattribute(pdo::attr_case,pdo::case_upper);$sql="select * from fate";$statement=$pdo->prepare($sql);$statement->execute();
2.$fetch_style的几种模式
// pdo::fetch_assoc$result=$statement->fetch(pdo::fetch_assoc);print_r($result);echo "<br>";// pdo::fetch_num$result=$statement->fetch(pdo::fetch_num);print_r($result);echo "<br>";// pdo::fetch_both$result=$statement->fetch(pdo::fetch_both);print_r($result);echo "<br>";// pdo::lazy$result=$statement->fetch(pdo::fetch_lazy);print_r($result);echo "<br>";// pdo::obj$result=$statement->fetch(pdo::fetch_obj);print_r($result);
输出:连接成功array([id] => 1[name] => saber[age] => 100)array([0] => 2[1] => acher[2] => 77)array([id] => 3[0] => 3[name] => luncher[1] => luncher [age] => 56[2] => 56)pdorow object([querystring] => select * from fate[id] => 4[name] => cooker[age] => 18)stdclass object([id] => 5[name] => 张三[age] => 66)
推荐:《2021年php面试题大汇总(收藏)》《php视频教程》
以上就是解析php中的pdo::fetch()方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product