MySQL只显示最后的数据

时间:2015-08-03 12:41:07

标签: php html mysql

我试图让网站从我的数据库中读取一个表,但它只显示我表的最后一行。此表用于测试目的,以了解如何读出我的数据库

我要做的是创建用户名时,表格会更新并显示在网站上。

在stackoverflow中搜索了正确的答案,但没有任何效果。我做错了什么?

这是我的代码:

  <body>

  <?php
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="members"; // Table name

    $conn = new mysqli($host, $username, $password, $db_name);

    // check connection
    if ($conn->connect_error) {
      trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
    }

    //select rows 
    $sql= 'SELECT id, username, country FROM members';
    $result = mysqli_query($conn, $sql);

   if (mysqli_num_rows($result) > 0) 
   {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) 
    {
        $f1 = $row['id'];
        $f2 = $row['username'];
        $f3 = $row['country'];
    }
   } else {
    echo "0 results";
   }

mysqli_close($conn);
?> 
    <table border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif">id</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">username</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">country</font>
      </td>
    </tr>
    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
      </td>
    <td>
  </body>

6 个答案:

答案 0 :(得分:2)

您只输出最后一行。你在这里阅读每一行:

while($row = mysqli_fetch_assoc($result)) 
{
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];
}

但是你正在将它们读入相同的变量。因此,每次循环迭代时,都会覆盖这些变量中的先前值。因此,这些变量将仅包含 last 行的数据。

输出步骤也放入循环中。在结构上,它看起来像这样:

/* output HTML table header here */
while($row = mysqli_fetch_assoc($result)) 
{
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];
    /* output HTML table row here */
}
/* output HTML table footer here */

答案 1 :(得分:1)

页面加载时将执行以下while循环。您在每次迭代中覆盖变量$f1$f2$f3,最后当程序退出while循环时,每个变量的值都是将是查询结果数组最后一行的idusernamecountry

// output data of each row
while($row = mysqli_fetch_assoc($result)) 
{
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];
}

你应该做的是,在这个while循环中添加表数据,就像这样

<?php
while($row = mysqli_fetch_assoc($result)) 
{
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];
 ?>

  <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
  </td>
   <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
  </td>
   <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
  </td>
<?php } ?>

答案 2 :(得分:0)

所有<TD>Variables $f1$f2$f3都必须在while循环中。

试试例

<body>

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

$conn = new mysqli($host, $username, $password, $db_name);

// check connection
if ($conn->connect_error)
{
    trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}

//select rows 
$sql= 'SELECT id, username, country FROM members';
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) 
{
?>

<table border="0" cellspacing="2" cellpadding="2">
    <tr>
        <td>
            <font face="Arial, Helvetica, sans-serif">id</font>
        </td>
        <td>
            <font face="Arial, Helvetica, sans-serif">username</font>
        </td>
        <td>
            <font face="Arial, Helvetica, sans-serif">country</font>
        </td>
    </tr>

<?php
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) 
    {
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];
    ?>
    <tr>
        <td>
            <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
        </td>
        <td>
            <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
        </td>
        <td>
            <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
        </td>
    </tr>
    <?php
    }
echo "</table>";
}
else
{
    echo "0 results";
}

mysqli_close($conn);
?> 

</body>

答案 3 :(得分:0)

您继续在while循环中重置$f1$f2$f3的值

试试这个:

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

$conn = new mysqli($host, $username, $password, $db_name);

// check connection
if ($conn->connect_error) {
    trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}

//select rows 
$sql= 'SELECT id, username, country FROM members';
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $Results = array();
    while($row = mysqli_fetch_assoc($result)) {
        $Results = array('f1' => $row['id'], 'f2' => $row['username'], 'f3' => $row['country']);
    }

    var_dump($Results);
} 
else {
echo "0 results";
}

mysqli_close($conn);

答案 4 :(得分:0)

您需要为每条记录输出一个表格行,例如像这样:

<table border="0" cellspacing="2" cellpadding="2">

    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif">id</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">username</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">country</font>
      </td>
    </tr>

<?php
   // output data of each row
 while($row = mysqli_fetch_assoc($result)) {
    $f1 = $row['id'];
    $f2 = $row['username'];
    $f3 = $row['country'];

    ?> 

    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
      </td>
    </tr>

<?php

}
 mysqli_close($conn);

?>
    </table>

答案 5 :(得分:0)

<body>
  <?php
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="members"; // Table name
    $conn = new mysqli($host, $username, $password, $db_name);
    // check connection
    if ($conn->connect_error) {
      trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
    }
    //select rows 
    $sql= 'SELECT id, username, country FROM members';
    $result = mysqli_query($conn, $sql);
    ?>
    <table border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif">id</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">username</font>
      </td>
      <td>
        <font face="Arial, Helvetica, sans-serif">country</font>
      </td>
    </tr><?php
   if (mysqli_num_rows($result) > 0) 
   {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) 
    {
        $f1 = $row['id'];
        $f2 = $row['username'];
        $f3 = $row['country'];
    ?>

    <tr>
      <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
      </td>
       <td>
        <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
      </td>
    </tr>
     <?php    } else {
    ?>
    <tr>
        <td colspan="3">NO Record Found</td>
    </tr>
    <?php
   }
   }
   mysqli_close($conn); ?> 
    </table>
  </body>