从MySQL中选择双重数据

时间:2014-11-19 06:04:17

标签: php mysql

嘿所以我试图从mysql数据库中获取数据,当我从mysql中获取数据时,我的表中数据加倍了?

以下是我用来制作图表的代码。

$query = "SELECT id,username FROM users";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
    foreach($row as $id => $val)
    {
        echo 
        "<tr>
        <td>" . $row['id'] . "</td>
        <td>" . $row['username'] . "</td>
        <td></td>
        <td></td>
        </tr>";
    }
}

这是结果: enter image description here

3 个答案:

答案 0 :(得分:1)

使用onli sing循环... while循环或foreach ...你正在使用两个循环,例如foreach inside..Ise that

$query = "SELECT id,username FROM users";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{

        echo 
        "<tr>
        <td>" . $row['id'] . "</td>
        <td>" . $row['username'] . "</td>
        <td></td>
        <td></td>
        </tr>";

}

答案 1 :(得分:1)

仅使用单循环

while($row = mysql_fetch_assoc($result))
{
      echo 
        "<tr>
        <td>" . $row['id'] . "</td>
        <td>" . $row['username'] . "</td>
        <td></td>
        <td></td>
        </tr>";
}

而不是双循环

while($row = mysql_fetch_assoc($result))
{
    foreach($row as $id => $val)
    {
        echo 
        "<tr>
        <td>" . $row['id'] . "</td>
        <td>" . $row['username'] . "</td>
        <td></td>
        <td></td>
        </tr>";
    }
}

答案 2 :(得分:1)

使用“mysql_fetch_array”而不是“mysql_fetch_assoc”:

$query = "SELECT id,username FROM users";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
    echo 
    "<tr>
    <td>" . $row['id'] . "</td>
    <td>" . $row['username'] . "</td>
    <td></td>
    <td></td>
    </tr>";
}