$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 150 banana yellow 250 kiwi brown 75 lemon yellow 25 orange orange 300 pear green 150 watermelon pink 90
先取得表的列名。SHOW COLUMNS FROM tbl_name
参考的官方例子