老师,分页那部分,我提示Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in E:\yifeng\core\Db.php on line 19
connect failed:Access denied for user ''@'localhost' (using password: NO)
public static function pagination($table, $where = '', $page = 1, $pagesize = 10, $order = ''){
$conn = self::db_connect();
$totals = self::totals($table, $where); // 总条数
$totalpage = ceil($totals / $pagesize); // 总页数
$page = max(1, $page); // 当前页
$offset = ($page - 1) * $pagesize; // 分页查询的开始位置
// 拼接sql
$sql = "select * from {$table}";
if ($where) {
$sql .= " WHERE ".$where;
}
if ($order) {
$sql .= " ORDER BY " . $order;
}
$sql .= " limit ".$offset.','.$pagesize;
$result = $conn->query($sql); // 执行sql
if ($result->num_rows > 0) { // 返回数据的条数
while ($row = $result->fetch_assoc()) { // 返回的一条条数据
$rows[] = $row;
}
$result->free_result(); // 释放结果内存
}
$conn->close();
return array(
'total' => $totals,
'page' => $page,
'pagesize' => $pagesize,
'list' => $rows
);
}
Alone882019-04-09 22:14:21
调用了两次db_connect, 一次是直接调用的db_connect , 第二次是total的,不知道为什么老师的没有出错,
一种办法, 在分页方法里面重新写一遍获取总数的方法赋值给变量
/** * @param $table * @param $where * @param $page * @param $num * @param string $order * @return array */ public static function pagination($table, $where, $page, $num, $order=''){ $conn =self::db_connect(); $count=0; $count_sql = "SELECT count(*) as count FROM {$table}"; if($where){ $count_sql .=" WHERE ".self::getwhere($where); } // exit($count_sql); if($count_result = $conn->query($count_sql)){ $row = $count_result->fetch_assoc(); $count = $row['count']; } $total_page = ceil($count/$num);//总页数 $page=max(1,$page); // 处理$page,max(min,max) 返回最大数 $offset = ($page-1)*$num;// 每页的起始数 // 拼接查询的SQL $sql = "SELECT * FROM {$table}"; if($where){ $sql .=' WHERE '.self::getwhere($where); } if($order){ $sql .= " ORDER BY {$order}"; } $sql .=" LIMIT {$offset} , {$num}"; $rows=[]; if($result = $conn->query($sql)){ while($row = $result->fetch_assoc()){ $rows[]=$row; } mysqli_free_result($result); } $conn->close(); return array('total'=>$count,'page'=>$page,'num'=>$num,'total_page'=>$total_page,'lists'=>$rows); }
振远2018-12-25 19:28:19
与同样的问题
Notice: Undefined variable: db in D:\xuexi\core\db.php on line 18
Notice: Undefined variable: db in D:\xuexi\ core\db.php,第 18 行
注意:未定义的变量:D:\xuexi\core\db.php 中的 db,第 18 行
注意:未定义的变量:D:\ 中的 db xuexi\core\db.php 第 18 行
警告:mysqli_connect(): (HY000/1045): D:\xuexi\ 中的用户 ''@'localhost' 访问被拒绝(使用密码:NO) core\db.php 第 18 行
连接失败:用户 ''@'localhost' 的访问被拒绝(使用密码:NO)