下面是代码,这里我使用了"从部门选择计数(*)"而不是使用"从部门选择*#34;所以我在这段代码中错了。任何人都可以帮我解决它..
$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> ";
答案 0 :(得分:1)
$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
好的,你应该选择。 选择计数()将返回单个数字(行数)。 //计算记录数
以后已经提取了。
总记录是从部门
中选择*记录数是来自部门的选择计数(*)
答案 1 :(得分:1)
我认为您唯一的问题是您尝试获取记录总数的方式。设置SQL查询以进行计数,然后通过读取返回的唯一行来检索它:
$sql = "SELECT count(*) AS total_records FROM departments";
$rs_result = $db_connection->query($sql);
$total_records = $rs_result->fetch_assoc()['total_records'];