search

Home  >  Q&A  >  body text

Pagination code encapsulates part of sql connection error

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_xinyuphpcn_xinyu2363 days ago1520

reply all(7)I'll reply

  • Alone88

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


    reply
    0
  • Alone88

    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 · 2019-04-10 08:13:56
    Alone88

    You can also change require_once to require

    Alone88 · 2019-04-10 08:17:29
  • 振远

    振远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)

    reply
    0
  • 、馬

    、馬2018-07-11 09:41:17

    hesitating

    reply
    1
  • 无忌哥哥

    无忌哥哥2018-07-11 09:24:01

    The database password is wrong.

    reply
    1
  • 、馬

    Think about it

    、馬 · 2018-07-11 09:41:59
  • Cancelreply