Php 显示来自mysql的数据,在表中显示

Php 显示来自mysql的数据,在表中显示,php,Php,我正在构建一个网站,它将从mysql返回结果并显示在网页上。 我在mysql中存储数据或检索数据方面没有问题 并将其显示在网页上。 当数据返回到页面时,我想将其存储在一个表中并运行一个循环,这样对于每个记录,都会有一个像列表编号一样递增的数字。 任何帮助都将不胜感激。 我的代码经常出错: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not con

我正在构建一个网站,它将从mysql返回结果并显示在网页上。 我在mysql中存储数据或检索数据方面没有问题 并将其显示在网页上。 当数据返回到页面时,我想将其存储在一个表中并运行一个循环,这样对于每个记录,都会有一个像列表编号一样递增的数字。 任何帮助都将不胜感激。 我的代码经常出错:

<?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("forloops", $con);

    $result = mysql_query("SELECT * FROM userinfo ORDER BY age DESC");

    echo "<table border='1'>
    <tr>
        <th>Position</th>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    </tr>";
    $counter = 0;
        $position= 0;
    $row = mysql_fetch_array($result);

    if($counter <=10){

            echo "<tr>";
                        echo "<td>" . $position . "</td>";
            echo "<td>" . $row['firstname'] . "</td>";
            echo "<td>" . $row['lastname'] . "</td>";
            echo "<td>" . $row['age'] . "</td>";
            echo "</tr>";
            $counter++;
            }
    echo "</table>";
    mysql_close($con);
    ?> 

当前代码的问题是,您只从查询中获取第一行,请看我是如何在下面记录的:

echo "<table border='1'>
<tr>
    <th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
    $position= 0;

while ($row = mysql_fetch_array($result)) { // This is new code
    if($counter <=10){

        echo "<tr>";
                    echo "<td>" . $position . "</td>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";
        echo "<td>" . $row['age'] . "</td>";
        echo "</tr>";
        $counter++;
    }
} // This is new code
echo "</table>";
mysql_close($con);
?> 
echo”
位置
名字
姓氏
年龄
";
$counter=0;
$position=0;
而($row=mysql\u fetch\u array($result)){//这是新代码
如果($柜台
如果您注意到了,我已经在表的行
和单元格
的回音周围创建了一个循环


while
循环将遍历查询返回的每一行。

如果数据库中有多个结果,则应使用:

 while ($row = mysql_fetch_array($result)) {
 //process one row
}
用你的方式,$row总是一样的。
第二,如果我看得对的话,就不要增加$position变量。

我是在你发布它的时候写的,所以很抱歉,重复:)