问题
I am trying to create pagination on some pages that display rows from a MySQL table. Each page displays six items from the table. I am using a while loop with:
$row_users = mysqli_fetch_array($result_users, MYSQL_NUM);
It just keeps looping through the results until there are no more (I would include the full code but it's part of a very large amount of elements). I have a GET variable from the page id as:
$page = $_GET['page'];
Basically I want to start the fetch array on the specific row for each page. For example on http://www.example.com?page=2 I would like it to start on the thirteenth row of the table (($page*6)+1).
My question is, how do I get a fetch_array to start on a certain row? I saw this post: Mysqli fetch array nth row but that just deals with grabbing the specific row, not starting the loop and continuing on past that row. I can't seem to find the method of doing this anywhere. Perhaps I'm Googling the wrong thing...thanks for your help!
回答1:
Rather than trying to skip the rows you don't want while fetching them, it's more efficient to tell MySQL not to produce those rows. Use the LIMIT clause at the end of your SELECT query to have MySQL only produce the rows you need:
http://dev.mysql.com/doc/refman/5.0/en/select.html
来源:https://stackoverflow.com/questions/13556497/mysqli-fetch-array-starting-the-query-on-a-certain-row