【问题标题】:PHP/MySQL query into html table issuePHP/MySQL 查询到 html 表问题
【发布时间】:2012-04-15 05:11:31
【问题描述】:

我在将查询放入要在 php/mysql 设置中显示的 html 表中时遇到了一个奇怪的问题。查询总是会产生比显示的结果多一个的结果。例如,如果返回一组结果,则表中不会显示任何内容。如果有 5 个,则仅显示 4 个。我已经使用预定义的函数尝试了相同的查询来生成基于查询的表,并且它生成了具有正确数量结果的正确表。这是一个示例(对缩进或缺少缩进表示歉意):

$user_id = implode($_SESSION['user_id']);
$query =    "SELECT name, address
    FROM user
    WHERE user_id = '$user_id' 
    ORDER BY date_added DESC";
$result = mysql_query($query) or die ("query failed: " . mysql_error());        

echo "<table border='1' style=\"border-collapse: collapse;\">
<tr style=\"background-color: #000066; color: #FFFFFF;\">
<th>Name</th>
<th>Address</th>
</tr>";

$rowCt = 0; // Row counter
while($row = mysql_fetch_array($result))
  {
  if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";
  else $Style = "background-color: #0099CC;";
  echo "<tr style=\"$Style\">";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";

知道为什么会这样吗?任何帮助将不胜感激,这让我发疯了,需要弄清楚才能继续前进!谢谢。

编辑

我已经稍微改变了它以循环遍历行数,因为该数字是正确的,并且表格现在是正确的大小,但是最后一个结果总是空的。有什么想法吗?

$rowCt = 0; // Row counter
    $i = 0;
    echo mysql_num_rows($result). ' number of rows';
    $num =  mysql_num_rows($result);
    //while($row = mysql_fetch_array($result))
    while ($i < $num)
    {

    $row = mysql_fetch_array($result);

    if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";
    else $Style = "background-color: #0099CC;";
    echo "<tr style=\"$Style\">";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['address'] . "</td>";
    echo "</tr>";
    $i++;

    }
    echo "</table>";

谢谢

【问题讨论】:

  • echo mysql_num_rows($result);并确保它返回您期望的行数
  • 你想用 'implode($_SESSION['user_id'])' 做什么?
  • 我认为您的代码没有任何问题。只有在你的时候,你有一个 if 和 else,试着把它们注释掉,看看会发生什么,它可能会做一些意想不到的事情。另外我建议直接在 phpmyadmin(如果已安装)中执行 $query 并查看返回的数据集以确认返回的数据。
  • 我试过回显行数,在这个例子中它会回显2但只显示一个结果集,很奇怪。 implode 语句是检索用户 id 以完成查询。我也尝试删除 if/else 无济于事,以及在 phpmyadmin 中执行查询,它返回相同的数字(比显示的多一个)。有任何想法吗?感谢您的回复

标签: php mysql html


【解决方案1】:

试试这个…………

<style>
#s {
background-color: #000066; 
color: #FFFFFF;
}
#s1 {
background-color: #0CC;
}
#s2 {
background-color: #09C;
}
</style>    
<?php
$user_id = implode($_SESSION['user_id']);
$result = mysql_query("SELECT name, address FROM user WHERE user_id = '$user_id' ORDER BY date_added DESC", $your_connection) or die ("query failed: " . mysql_error());        
?>
<table border="1" style="border-collapse:collapse;">
<tr id="s">
<th>Name</th>
<th>Address</th>
</tr>
<?php
$rowCt = 0; // Row counter
while($row = mysql_fetch_assoc($result))
  {
  if($rowCt++ % 2 == 0):
    $Style = "s1";
  else: 
    $Style = "s2";
  endif;
  ?>
  <tr id="<?php echo $Style; ?>">
  <td><?php $row['name']; ?></td>
  <td><?php $row['address']; ?></td>
  </tr>
  <?php } ?>
  </table>

【讨论】:

    【解决方案2】:

    您是否在页面上声明了DOCTYPE?这是我唯一能想到的。您的代码看起来正确。

    注意:如果没有指定 !DOCTYPE,border-collapse 属性可以 产生意想不到的结果。

    http://www.w3schools.com/cssref/pr_tab_border-collapse.asp

    另外,您是否打开了警告/错误?最后一行可能有一些错误,这就是它不显示的原因。

    【讨论】:

    • 您好,感谢您的回复。文档类型已声明,并且没有与该区域相关的错误显示。即使我得到它来计算返回的东西的数量,它仍然显示比实际少一!还有其他想法吗?
    【解决方案3】:

    尝试使用mysql_fetch_assoc 而不是mysql_fetch_array

    【讨论】:

      【解决方案4】:

      在 while 循环中检查您的 $i 和 $num。 我认为你的时间应该是:

      while($i <= $num){
      // Your code
      }
      

      【讨论】:

        【解决方案5】:

        试试这个:

            <?php
        $user_id = $_SESSION['user_id'];
        $query = mysql_query("SELECT * FROM `user` WHERE `user_id`='$user_id' ORDER BY `date_added` DESC")or die(mysql_error());
        ?>
        <table border="1" style="border-collapse: collapse;">
        <tr style="background-color: #000066; color: #FFFFFF;">
        <th>Name</th>
        <th>Address</th>
        </tr>
        <?
        $colour2 = "transparent";  
        $colour1 = "#0099CC";  
        $row_count = 0; 
        while($row = mysql_fetch_array($query)){
        $rowcolor = ($row_count % 2) ? $colour1 : $colour2;
        ?>
        <tr style="background: <? echo $rowcolor; ?>;">
        <td><? echo $row['name']; ?></td>
        <td><? echo $row['address']; ?></td>
        </tr>
        <?
        $row_count++;
        }
        ?>
        </table>
        

        【讨论】:

          【解决方案6】:

          你试过没有表格的输出吗?即

          echo $row['name']." - ".$row['address']."<br />";
          

          如果结果相同,您是否在 phpMyAdmin 中应用了该查询来检查结果?可能是您的最后一条记录在数据库中为空,因此代码不显示任何内容。

          【讨论】:

            【解决方案7】:

            尝试使用 for 循环而不是 while。

            for($i=0;$i<=mysql_num_rows;$i++)
            {
                $row = mysql_fetch_array($query);
                ...
                ...
            }
            

            【讨论】:

              【解决方案8】:

              也许是算数

                  <th>Name</th>
              <th>Address</th>
              

              随着行的尝试,将它们取出。

              【讨论】:

                猜你喜欢
                • 2011-04-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-06-12
                相关资源
                最近更新 更多