Re: [請益] 讀取資料庫內資料表的內容存進html的ta …
※ 引述《s792935 (阿葆)》之銘言:
: 我想要從"test"資料庫中的"road"資料表中取出5個欄位的資料存進自己所畫的table裡
: 5個欄位分別是"category","location","description","date","time"
: 因為資料表內的資料筆數不固定(因為資料庫更新後讀取進來的資料筆數會被影響到)
: 如何寫出從資料庫內讀取五個欄位分別進入所畫的table裡的五個欄位呢?
: 以下是我用DW寫的php程式,現在只能顯示出第一筆資料在table裡
: 想請問各位是否在哪邊需再加上判斷式(while迴圈)?
: 謝謝各位的解答~
: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
: "" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
: <html xmlns="" rel="nofollow">http://www.w3.org/1999/xhtml">
: <?php
: mysql_pconnect("localhost","帳號","密碼"); //連結資料庫
: mysql_select_db("test");//選擇資料庫
: $result = mysql_query("SELECT * FROM road"); //取得資料表
/*
: $row_result = mysql_fetch_assoc($result); //取得資料表內的資料
你這樣寫只會跑出一筆資料
把這行移掉
*/
: ?>
: <head>
: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
: <title>國道甜心</title>
: </head>
: <body>
: <table width="1040" height="55" border="1" align="center" cellpadding="0"
: cellspacing="0">
: <tr bgcolor="#0099FF">
: <td width="111" bgcolor="#0099FF"><div align="center">類別</div></td>
: <td width="161"><div align="center">地點</div></td>
: <td width="546" bgcolor="#0099FF"><div align="center">路況說明</div></td>
: <td width="105" bgcolor="#0099FF"><div align="center">日期</div></td>
: <td width="105" bgcolor="#0099FF"><div align="center">時間</div></td>
: </tr>
<?php
while( $row_result = mysql_fetch_assoc($result) ):
?>
: <tr>
: <td><div align="center">
: <?php $category="category"; echo $row_result[$category]; ?>
: </div></td>
: <td><div align="center">
: <?php $location="location"; echo $row_result[$location]; ?>
: </div></td>
: <td><?php $description="description"; echo $row_result[$description];
: ?></td>
: <td><div align="center">
: <?php $date="date"; echo $row_result[$date]; ?>
: </div></td>
: <td><div align="center">
: <?php $time="time"; echo $row_result[$time]; ?>
: </div></td>
: </tr>
<?php endwhile;?>
: </table>
: </body>
: </html>
就這樣 ~
不過, 遇到這種問題 ~ 先去爬一下文或google一下
遇到函數不會的就上 http://www.php.net/ 這裡查一下
另外我個人習慣在 html 跟 php 交雜混在一起寫時
會用 while: , endwhile, if: , endif 之類的
因為一堆大括號我眼睛會花掉, 個人習慣, 給您一個參考
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.124.82.49
推
03/22 22:19, , 1F
03/22 22:19, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章