php mysql如何輸出查詢到的內容

來源:互聯網
上載者:User

比如我用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
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.