Jump to content


Photo

PHP or MySQL stripping first record from each page


  • Please log in to reply
3 replies to this topic

#1 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 06 March 2006 - 12:01 PM

Hi All,

You can see the script functioning [a href=\"http://www.skylinesaustralia.com/trader_convert.php\" target=\"_blank\"]here[/a]

But, if you click through a few pages, you'll see that the first record is never displayed. Ie, record 1, record 51, record 101 etc... I have no idea why...

Heres my code;

//snip

db_connect();

//if (!isset($_POST['screen'])) {
//    $screen = 0;
//} else {
$screen = $_REQUEST['screen'];

$rows_per_page = 50;
$start = $screen * $rows_per_page;
$q = "SELECT * FROM user_rate_trade ORDER BY userrateid LIMIT $start , $rows_per_page";
$r = mysql_query($q)or die(mysql_error());
$row = mysql_fetch_assoc($r);

while($row = mysql_fetch_assoc($r)) {

    $id = $row['userrateid'];
    $date = strtotime($row['userdate']);
    $to_id = $row['ratedused'];
    $userid = $row['userid'];
        if ($row['userrating'] == -1) {
        $rating = 2;
        } else if ($row['userrating'] == 0) {
        $rating = 3;
        } else if ($row['userrating'] == 1) {
        $rating = 1;
        }


    echo ''.$id.'. '.$row['userdate'].' = '.$date.' - Rating - '.$rating.'<br/>';

}

$newscreen = $screen + 1;
echo ''.$q.'<br />
<a href="trader_convert.php?screen='.$newscreen.'">Next</a>
';

Thanks for any help, it has me stumped.

Christian
The way to a mans stomach is through his heart.

#2 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 06 March 2006 - 02:23 PM

$r = mysql_query($q)or die(mysql_error());
$row = mysql_fetch_assoc($r);

while($row = mysql_fetch_assoc($r)) {

You have the above code. The reason you lose the first record is because of that first call to mysql_fetch_assoc. Change the code to this :

$r = mysql_query($q)or die(mysql_error());

while($row = mysql_fetch_assoc($r)) {

--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 06 March 2006 - 02:44 PM

It's amazing how often people make this mistake. mysql_fetch_array() iterates through a resource, every time you call it, you advance one record, so you don't want to call it until you're ready for a row.

#4 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 06 March 2006 - 11:17 PM

Thanks guys!

I should have picked that up..

Christian
The way to a mans stomach is through his heart.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users