Teacher, for the paging part, I prompted 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); /Total number of items
$totalpage = ceil($totals / $pagesize); //Total number of pages
$page = max(1, $page); //Current page
$offset = ($ page - 1) * $pagesize; // Starting position of paging query
// Splicing sql
$sql = "select * from {$table}";
if ($where) {
$sql .= " WHERE ".$where;
}
if ($order) {
$sql .= " ORDER BY " . $order;
}
$sql .= " limit ".$offset.','.$pagesize;
$result = $conn->query($sql); // Execute sql
if ($result->num_rows > 0) { // Number of returned data
. # $result->free_result(); // Release result memory
}
$conn->close();
return array(
'total' => $totals,
Alone882019-04-09 22:14:21
I called db_connect twice, once directly called db_connect, and the second time called total. I don’t know why the teacher didn’t make an error.
One way is to rewrite it in the paging method to get the total number. The method assigns the value to the variable
/** * @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 on line 18
Notice: Undefined variable: db in D:\xuexi\core\db.php on line 18
Notice: Undefined variable: db in D:\xuexi\core\db.php on line 18
Warning: mysqli_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in D:\xuexi\core\db.php on line 18
Connection failed:Access denied for user ''@'localhost' (using password: NO)