Jump to content

PHP MySqli does not fetch last row from the table.


angel1987

Recommended Posts

I noticed a bug in my script and i have been searching a lot on internet since past few hours but i could not find any information or solution. The script runs fine, no errors, but the script does not fetch last row/entry. Example, if there are 3 rows in the table, my script only pulls 2 rows. If there are 10, then only 9 are fetched. Please help me find the bug.

 

Here is my script...

<?php
$query = "SELECT * FROM members WHERE $condition_res ORDER BY id DESC LIMIT $startpoint, $limit";
$result_query = mysqli_query($mysqli, $query);  

while($row = @mysqli_fetch_assoc($result_query)) {
     $mid=$row["id"];
     $member=$row["name"];
     $gender= $row["gender"];
?>
       <tr>
           <td><?php echo $mid; ?></td>
           <td><?php echo $member; ?></td>
           <td><?php echo $gender; ?></td>
       </tr>
<?php } ?>

Note: $startpoint and $limit variables are for Pagination. $condition_res is for search criteria.

Link to comment
Share on other sites

Totally guessing because of the dearth of code, but I suspect it has to do with your pagination logic, which you do not show.

 

Why are you suppressing errors on your mysqli_fetch_assoc? Why aren't you using prepared statements? Are you sanitizing your conditional input?

Edited by boompa
Link to comment
Share on other sites

Have you looked closely at the values for the LIMIT clause?

 

 

LIMIT $startpoint, $limit

 

I suspect it might be an offset issue. The following quote was pulled from http://dev.mysql.com/doc/refman/5.0/en/select.html:

 

 

 

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.