比如我用select查询某个表中的某个id的字段。
结果类似
问题来了:请问如何输出获取到的这条数据?
格式类似:
id:xxx
product_id :xxx
number:xxx
...
注意点:
我并不知道里面有product_id等字段的名称也就是不能通过 类似 while后用 $xxx['product_id']的方式输出
回复内容: 比如我用select查询某个表中的某个id的字段。
结果类似
问题来了:请问如何输出获取到的这条数据?
格式类似:
id:xxx
product_id :xxx
number:xxx
...
注意点:
我并不知道里面有product_id等字段的名称也就是不能通过 类似 while后用 $xxx['product_id']的方式输出
先取得表的列名。show columns from tbl_name
$data = $mysql->query(select * from $table_name where id = $id);foreach($data as $k){//这里是结果集 foreach($k as $kk=>$vv){ echo $kk.':'.$vv.'
';//避开键名写死的情况 }}
参考的官方例子
a nice feature of pdo::query() is that it enables you to iterate over
the rowset returned by a successfully executed select statement.
query($sql) as $row) { print $row['name'] . \t; print $row['color'] . \t; print $row['calories'] . \n; }}?>----------apple red 150banana yellow 250kiwi brown 75lemon yellow 25orange orange 300pear green 150watermelon pink 90