Pagination code encapsulates part of sql connection error
phpcn_xinyu
phpcn_xinyu 2018-07-11 00:39:13
0
7
1495

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,
                                                                                                                                                                                                                                                                           

phpcn_xinyu
phpcn_xinyu

reply all(4)
Alone88

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);
    }


  • reply The main problem is that db_connect imports the database configuration. The second call may not be able to call it. If it is not used, setting the database configuration directly in db_connect will not cause this problem.
    Alone88 author 2019-04-10 08:13:56
  • reply You can also change require_once to require
    Alone88 author 2019-04-10 08:17:29
振远

跟我的一样的问题
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)

、馬

hesitating

无忌哥哥

The database password is wrong.

  • reply Think about it
    、馬 author 2018-07-11 09:41:59
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template