dpxyfa4718
2015-11-03 18:31 阅读 35
已采纳

在网页上显示MySql记录,PHP [关闭]

I’m trying to return the last 10 records from my database and display them on a web page. The code I have below only returns one record, does anyone know where I’m going wrong? I’ll tidy it up later with a table, for the moment I just need the last 10 records.

The name of the table is “devel” and the column names are: id, STATION, REASON_CODE, ERROR, WEEK, COMMENT, DOWNTIME, DATE

$dbc = mysqli_connect(‘xxxx’, ‘xxxx’, 'xxxx', ‘xxxx’)
    or die('Error connecting to MySQL server.');

$query = "SELECT * FROM devel ORDER BY id DESC LIMIT 10";

$result = mysqli_query($dbc, $query)
    or die('Error querying database.');

while($record = mysqli_fetch_array($result)){
echo $record['id']. " " .$record['STATION'] . " " . $record['REASON_CODE'] . " " . $record['ERROR']. " " .$record['WEEK'] . " " .$record['COMMENT'] . " "    .$record['DOWNTIME'] . " " .$record['DATE'];
echo "<br /";
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongping9475 dongping9475 2015-11-03 18:53

    Try this out

    $dbc = mysqli_connect(‘xxxx’, ‘xxxx’, 'xxxx', ‘xxxx’)
        or die('Error connecting to MySQL server.');
    $query = "SELECT * FROM devel ORDER BY id DESC LIMIT 10"; 
    $result = mysqli_query($dbc, $query)
        or die('Error querying database.');
    echo "<table><tr>
                     <th>Id</th>
                     <th>STATION</th>
                     <th>REASON_CODE</th>
                     <th>ERROR</th>
                     <th>WEEK</th>
                     <th>COMMENT</th>
                     <th>DOWNTIME</th>
                     <th>DATE</th>
                </tr>";
    while($record = mysqli_fetch_assoc($result)){
     echo "<tr>"; 
     echo "<td>".$record['id']."</td>";
     echo "<td>".$record['STATION']."</td>";
     echo "<td>".$record['REASON_CODE']."</td>";
     echo "<td>".$record['ERROR']."</td>";
     echo "<td>".$record['WEEK']."</td>";
     echo "<td> ".$record['COMMENT']."</td>";
     echo "<td> ".$record['DOWNTIME']."</td>";
     echo "<td> ".$record['DATE']."</td>";
     echo "</tr>";  
    }
     echo "</table>";
    
    点赞 评论 复制链接分享

相关推荐