Rumah > Soal Jawab > teks badan
比如我用SELECT查询某个表中的某个ID的字段。
结果类似
问题来了:请问如何输出获取到的这条数据?
格式类似:
id:xxx
product_id :xxx
number:xxx
...
注意点:
我并不知道里面有product_id等字段的名称也就是不能通过 类似 while后用 $xxx['product_id']的方式输出
伊谢尔伦2017-04-10 16:31:36
$data = $mysql->query("select * from $table_name where id = $id");
foreach($data as $k){//这里是结果集
foreach($k as $kk=>$vv){
echo $kk.':'.$vv.'<br/>';//避开键名写死的情况
}
}
参考的官方例子
A nice feature of PDO::query() is that it enables you to iterate over
the rowset returned by a successfully executed SELECT statement.
<?php
function getFruit($conn) {
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($conn->query($sql) as $row) {
print $row['name'] . "\t";
print $row['color'] . "\t";
print $row['calories'] . "\n";
}
}
?>
----------
apple red 150
banana yellow 250
kiwi brown 75
lemon yellow 25
orange orange 300
pear green 150
watermelon pink 90