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.

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?

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):

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.