0

我正在尝试获取 MySQL 表中的所有行并将它们放入 HTML 表中:

<table border="1" cellspacing="2" cellpadding="2">
    <tr>
        <td>Exam ID</td>
        <td>Status</td>
        <td>Assigned Examiner</td>
        <td>Recruit's name</td>
    </tr>
    <?php
        $query = mysql_query("SELECT * FROM `access`");
        $num = mysql_num_rows($query);
        $r = mysql_fetch_assoc($query);
    ?>
    <?php for($i=0;$i<$num;$i++) { ?>
    <tr>
    <?php echo '<td>' . $r['code'] . '</td>'; } ?> 
    </tr>
</table>

它输出如下:

Exam ID <br />
1<br />
1<br />
1<br />

很明显,我希望它像 MySQL 一样在不断增加。

4

3 回答 3

2

您只获取第一行。

放入mysql_fetch_assoc循环:

                <?php for($i=0;$i<$num;$i++) { 
                $r = mysql_fetch_assoc($query);
                ?>
                <tr>
                <?php echo '<td>' . $r['code'] . '</td>'; } ?> 
                </tr>

请注意,还有更多最新功能可供使用(请参阅文档中的警告)。

于 2013-01-19T20:17:56.997 回答
0

我会使用一个while循环:

<tr>
<?php 
while($r = mysql_fetch_assoc($query){

    echo '<td>' . $r['code'] . '</td>';

}
?>
</tr>

另请注意,mysql_php 命令很快就会被贬值。

于 2013-01-19T20:25:40.703 回答
0

<table border="1" cellspacing="2" cellpadding="2">
    <tr>
        <td>Exam ID</td>
        <td>Status</td>
        <td>Assigned Examiner</td>
        <td>Recruit's name</td>
    </tr>
    <?php
        // The query
        $query = 'SELECT exam_id, status, examine_name, recruiter_name FROM access';

        // The counter
        $number_of_rows = 0;

        //Run, Fetch and print
        $result = mysql_query('SELECT exam_id, status, examine_name, recruiter_name FROM access');
        while($row = mysql_fetch_assoc($query)){
            echo '<tr>';
            foreach($column as $column_name => $column_value){
                printf('<td>%s</td>', $column_value);
            }
            echo '</tr>';
            $number_of_rows++;
        }
    ?>
</table>

<p class="information">
    <?php printf('The query returned %d rows.', $number_of_rows);?>
</p>
于 2013-01-19T20:40:34.160 回答