簡體   English   中英

mysql FOUND_ROWS()返回錯誤的值

[英]mysql FOUND_ROWS() return wrong value

我試圖在我的查詢中使用FOUND_ROWS(),但函數有時會返回錯誤的值。

SELECT SQL_CALC_FOUND_ROWS adminslog.*, admins.fullName FROM adminslog 
JOIN admins ON admins.id=adminslog.userId ORDER BY date DESC LIMIT 0,12

在這個查詢中,我得到了一些正確的值,但在其他情況下,限制的值是錯誤的。

LIMIT 0,12        164rows        right
LIMIT 12,12       164rows        right
LIMIT 36,12       164rows        right
LIMIT 48,12       164rows        right
LIMIT 50,12       60rows         wrong
LIMIT 62,12       60rows         wrong

這是我的類構造:

class list_table
{
public $number,$page_number,$all_rec,$table_rows,$query_str,$query,$fetch,$table,$db,$fields,$i=0,$page_n,$onclick;

function __construct($query_str,$fields,$page_n,$onclick,$page_number,$number,$db)
{
    $this->fields = $fields; $this->page_number = (((int)$page_number<1)?1:(int)$page_number); $this->number = (int)$number; $this->db = $db;
    $this->i = $this->page_number*$this->number-$this->number; $this->page_n = $page_n; $this->onclick = $onclick;
    $this->query_str = substr(trim($query_str),0,7)."SQL_CALC_FOUND_ROWS ".substr(trim($query_str),7)." LIMIT ".(($this->page_number*$this->number)-$this->number).",".$this->number;

    $this->query = $this->db->query($this->query_str);
    $this->table_rows = $this->db->query("SELECT FOUND_ROWS()")->fetchColumn();

    $this->all_rec = $this->query->rowCount();
    $this->fetch = $this->query->fetch();

    //$this->table_rows = $this->table_rows->fetch();
    //$this->table_rows = $this->table_rows['cnt'];
    print $this->table_rows;
}

other functions...
}

mysql錯誤可能導致此問題,具體取決於您使用的版本: http//bugs.mysql.com/bug.php?id = 1468

您可以通過在查詢中使用GROUP BY子句來解決此問題。

問題:您認為每次查詢時您的第一個FOUND_ROWS()都是正確的嗎?

檢查一下,如果確實如此,您只能在第一個查詢中運行此代碼並將其保存到會話中。

if($this->page_number==1) $_SESSION['cr'] = $this->table_rows = $this->db->query("SELECT FOUND_ROWS()")->fetchColumn();

這樣你就不必每次都檢查行數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM