[英]Can we use count(*) in pagination rather then using count to get number of records in the table?
下面是代码,在这里我使用了“从部门中选择count(*)”,而不是“从部门中选择*”,所以我在这段代码中错了什么。 谁能帮我整理一下。
$num_rec_per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $num_rec_per_page;
$sql_query = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
$sql = "SELECT count(*) FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index.php?page=$total_pages'>".'>|'."</a> ";
$sql = "SELECT count(*) FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
好的,您应该选择。 选择count( )将返回一个数字(行数)。 //记录数
稍后已将其提取出来。
总记录从部门中选择*
记录数是部门的选择计数(*)
我认为您唯一的问题是您尝试获取记录总数的方式。 设置SQL查询以进行计数,然后通过读取返回的唯一行来检索它:
$sql = "SELECT count(*) AS total_records FROM departments";
$rs_result = $db_connection->query($sql);
$total_records = $rs_result->fetch_assoc()['total_records'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.