pdostatement::fetchall-k8凯发旗舰
array(3) { ["apple"]=> array(2) { [0]=> string(5) "green" [1]=> string(3) "red" } ["pear"]=> array(2) { [0]=> string(5) "green" [1]=> string(6) "yellow" } ["watermelon"]=> array(1) { [0]=> string(5) "pink" } }
example #4 每行结果实例化一个类
下面列子演示了 pdo::fetch_class
获取风格的行为。
class fruit {
public $name;
public $colour;
}
$sth = $dbh->prepare("select name, colour from fruit");
$sth->execute();
$result = $sth->fetchall(pdo::fetch_class, "fruit");
var_dump($result);
?>
以上例程的输出类似于:
array(3) { [0]=> object(fruit)#1 (2) { ["name"]=> string(5) "apple" ["colour"]=> string(5) "green" } [1]=> object(fruit)#2 (2) { ["name"]=> string(4) "pear" ["colour"]=> string(6) "yellow" } [2]=> object(fruit)#3 (2) { ["name"]=> string(10) "watermelon" ["colour"]=> string(4) "pink" } [3]=> object(fruit)#4 (2) { ["name"]=> string(5) "apple" ["colour"]=> string(3) "red" } [4]=> object(fruit)#5 (2) { ["name"]=> string(4) "pear" ["colour"]=> string(5) "green" } }
example #5 每行调用一次函数
下面列子演示了 pdo::fetch_func
获取风格的行为。
function fruit($name, $colour) {
return "{$name}: {$colour}";
}
$sth = $dbh->prepare("select name, colour from fruit");
$sth->execute();
$result = $sth->fetchall(pdo::fetch_func, "fruit");
var_dump($result);
?>
以上例程的输出类似于:
array(3) { [0]=> string(12) "apple: green" [1]=> string(12) "pear: yellow" [2]=> string(16) "watermelon: pink" [3]=> string(10) "apple: red" [4]=> string(11) "pear: green" }#pdo #php