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 に答える