首頁  >  問答  >  主體

分頁代碼封裝部分sql連線報錯

老師,分頁那部分,我提示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);  $totals = self::  $table, $where);    / 總條數
   $totalpage = ceil($totals / $pagesize);      // 總頁數
   $page = max(1, $page);   an  $ page - 1) * $pagesize;                  // 分頁查詢的起始位置
   // 拼接sql
   $ $sql .= " WHERE ".$where;
   }
   if ($order) {
       $sql .= " ORDER BY " . $ " 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(## c ;close();
 tal)  ##        'page'          => $page,
       'pagesize'      => $pagesize,
  c   );
}

phpcn_xinyuphpcn_xinyu2267 天前1444

全部回覆(7)我來回復

  • Alone88

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


    回覆
    0
  • Alone88

    主要是db_connect 導入資料庫配置問題,第二次呼叫可能呼叫不出來,如果不掉用,直接在db_connect裡面設定資料庫配置就不會這樣..

    Alone88 · 2019-04-10 08:13:56
    Alone88

    也可以把require_once 改成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,第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)

    回覆
    0
  • 、馬

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

    吞吞吐吐

    回覆
    1
  • 无忌哥哥

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

    資料庫密碼錯了。

    回覆
    1
  • 、馬

    想想

    、馬 · 2018-07-11 09:41:59
  • 取消回覆