PHP查询未在html表

时间:2016-03-16 19:50:28

标签: php html mysql

所以我有以下查询来显示我的数据库中的所有用户:

<ul class="names">
                    <table>
                        <tr>
                            <th>Navn</th>
                            <th>Email</th>
                            <th>Score</th>
                        </tr>
                    <?php
                    $connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
                    mysql_select_db('users');

                    $query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
                    $result = mysql_query($query);

                    $row=mysql_fetch_array($result);

                    while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
                    echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>";  //$row['index'] the index here is a field name
                    }

                    mysql_close(); //Make sure to close out the database connection
                    ?>
                    </table>
                </ul>

但由于某种原因,它并没有显示所有数据。

仅显示具有iUserId 2的用户而不是一个用户。

有没有人知道可能出现什么问题?

我可以使用iUserId 1的凭据登录,并在登录页面上显示正确的信息。

但不在这里:S

3 个答案:

答案 0 :(得分:7)

删除第$row = mysql_fetch_array($result);

因为此行开始从查询结果中提取记录。首次获取的记录是标识为1的记录,您不会对其执行任何操作。

然后您开始echo其他记录,但已跳过ID为1的记录。

答案 1 :(得分:3)

在定义while循环之前获取第一行;一旦你进入while循环,它就会获取下一行,这是第二行。删除第一个mysql_fetch_array($result)操作。

顺便说一下,不推荐使用PHP中的原始mysql api,建议改为使用mysqli

答案 2 :(得分:0)

我认为你应该这样说吧

    <?php
                        $connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
                        mysql_select_db('users');
let all the connection be outside the "ul" tag.

$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
                        $result = mysql_query($query);
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
                        $result = mysql_query($query);

                        $row=mysql_fetch_array($result);

    ?>
<ul class="names">
                    <table>
                        <tr>
                            <th>Navn</th>
                            <th>Email</th>
                            <th>Score</th>
                        </tr>
                    <?php

                    while($row){   //Creates a loop to loop through results
                    echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>";  //$row['index'] the index here is a field name
                    }

                    mysql_close(); //Make sure to close out the database connection
                    ?>
                    </table>
                </ul>