How to count all records but only retrieve (LIMIT) a specific number for display?

后端 未结 4 1152
我在风中等你
我在风中等你 2021-01-13 12:29

I want to echo only the first 10 rows, but I need to count the total number of rows affected by the query.

I was doing a LIMIT 10 and then counting with

4条回答
  •  长情又很酷
    2021-01-13 13:02

    You have to make 2 queries: the first one will count all rows, the second one will return 10 rows:

    $count = 0;
    $query = "SELECT count(*) as count FROM Badges WHERE UID = '$user'";
    $rs = mysql_query($query);   
    
    if (mysql_errno() == 0)
    {
        $r = mysql_fetch_object($rs);
        $count = $r->count;
    }
    
    if ($count > 0)
    {
        $query = "SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 10";
        $rs = mysql_query($query);      
    
        if (mysql_errno() == 0)
        {            
            while ($r = mysql_fetch_array($rs))       
            {       
                echo $r['Site']; 
            }     
        }
    }
    

提交回复
热议问题