函数名称:PDOStatement::fetch()
适用版本:PHP 5 >= 5.1.0, PHP 7, PHP 8
函数描述:从结果集中获取下一行作为关联数组、数字索引数组或两者兼有的形式
用法: PDOStatement::fetch(int $fetch_style = PDO::FETCH_BOTH, int $cursor_orientation = PDO::FETCH_ORI_NEXT, int $cursor_offset = 0) : mixed|false
参数:
$fetch_style(可选):指定返回的数组类型,可选值包括:
$cursor_orientation(可选):指定游标移动的方式,可选值包括:
$cursor_offset(可选):游标偏移量,默认为0,表示不偏移
返回值:
示例: <?php // 假设已经创建了一个名为 $stmt 的 PDOStatement 对象
// 获取关联数组 $row = $stmt->fetch(PDO::FETCH_ASSOC); print_r($row);
// 获取数字索引数组 $row = $stmt->fetch(PDO::FETCH_NUM); print_r($row);
// 获取关联数组和数字索引数组 $row = $stmt->fetch(PDO::FETCH_BOTH); print_r($row);
// 获取以属性名对应列名的对象 $row = $stmt->fetch(PDO::FETCH_OBJ); var_dump($row); ?>
注意事项: