php - 使用数据表时,表中的记录计数无法正确显示

标签 php mysql sql datatable

我正在根据查询返回的结果动态生成一个表,一切都很好,表正在生成,数据表显示正确。但是记录数是错误的。这里的问题是,如果我得到 10 条记录计数中显示 9 条记录。但表中存在 10 条记录。 请在这方面帮助我。 我的代码: PHP 代码

mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
echo $totalRows_getcolumns;

表格代码

if (($getcolumns)||(mysql_errno == 0))  
{  
  echo "<table width='50%' class='table table-striped table-bordered table-hover' align='center' id='sample_2'>
  <thead><tr>";  
  if (mysql_num_rows($getcolumns)>0)  
  {  
          //loop thru the field names to print the correct headers  
          $i = 0;  
          while ($i <  mysql_num_fields($getcolumns))  
          {  
       echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";  
       $i++;  
    }  
    echo "</tr></thead>";
    echo "<tbody>";

    //display the data  
    while ($rows =  mysql_fetch_assoc($getcolumns))  
    {  
      echo "<tr>";  
      foreach ($rows as $data)  
      {  
        echo "<td align='center' width=''>". $data . "</td>";  
      }  
    }  
  }else{  
    echo "<td colspan='" . ($i+1) . "'>No Results found!</td></tr>";  
  }  
  echo "</tbody></table>";  
}else{  
  echo "Error in running query :". mysql_error();  
}  

最佳答案

我相信你的 PHP 代码,第四行正在做的问题是

$row_getcolumns = mysql_fetch_assoc($getcolumns);

实际上,当您调用 mysql_fetch_assoc 时,它会从顶部获取记录并将内部指针移动到下一条记录。

因此,您在创建表之前就已经丢失了一条记录

这行代码在您的代码中似乎没有用。评论此行并尝试。

关于php - 使用数据表时,表中的记录计数无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227240/

相关文章:

mysql - 如何在 MySQL 的 WHERE 子句中使用计算字段?

javascript - 动态创建的表无法识别无序列表标签

PHP+MySQL软件

c# - 来自 Access ODBC 驱动程序的无效日期时间格式异常

mysql - 选择在日期范围内花费超过一定金额的客户

javascript - 如何使用 for 循环在带有可编辑 tr 的 html 表中附加 json 输出

javascript - jquery ajax 添加到mysql数据库

javascript - 使用 PHP 验证查找恶意 PDF 文件?

php - 无需支付的电子购物系统

mysql - 根据结果​​集中的结果编号更新行