PDO:getting 回显 fetch 和 fetchAll 的结果
PDO:getting around echoing result of fetch and fetchAll
我有一个 table,在 it.I 中有四个字段可以更改我的查询以显示我想要的内容,也就是说,四个 fields.I 中的两个想要 php 为我做这个。
$stmt=$db->query("select * from remote");
$result=$stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $row){
echo $row;
}
这给了我 table 中的第一个 'row' 以及 it.My 中的所有元素 table 类似于
--------------------------
|id|name|position|my_box|
--------------------------
|1 |bear|3 |5 |
目前我有 4 个 rows.With fetchAll 我不知道如何以可读的方式回应它们(不使用 print_r)。我如何打印,Id=>Value并且只有 Id 或 Value。
获取行 while
它们存在:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'], ' - ' ,$row['name'], '<br />';
}
<?php
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
foreach ($row as $field => $value) {
echo $field . ' => ' . $value . '<br>';
}
}
我有一个 table,在 it.I 中有四个字段可以更改我的查询以显示我想要的内容,也就是说,四个 fields.I 中的两个想要 php 为我做这个。
$stmt=$db->query("select * from remote");
$result=$stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $row){
echo $row;
}
这给了我 table 中的第一个 'row' 以及 it.My 中的所有元素 table 类似于
--------------------------
|id|name|position|my_box|
--------------------------
|1 |bear|3 |5 |
目前我有 4 个 rows.With fetchAll 我不知道如何以可读的方式回应它们(不使用 print_r)。我如何打印,Id=>Value并且只有 Id 或 Value。
获取行 while
它们存在:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'], ' - ' ,$row['name'], '<br />';
}
<?php
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
foreach ($row as $field => $value) {
echo $field . ' => ' . $value . '<br>';
}
}