比如我用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