我正在根据查询返回的结果动态生成一个表,一切都很好,表正在生成,数据表显示正确。但是记录数是错误的。这里的问题是,如果我得到 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/