MySQL results skipping first row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

MySQL results skipping first row

Post by iG9 »

No idea what this is about. If I run the query at the console I get 13 results, but the script only returns 12, starting with number 2.

Code: Select all

 
function getListings($q = 'select * from properties') {
    $r = mysql_query($q);
        
    $numRows = mysql_num_rows($r);
    
        $r = mysql_query($q);
        $d = mysql_fetch_object($r);
        
        echo "<table><tr>";
        $row = 0;
        $numRows = mysql_num_rows($r);
        while ($d = mysql_fetch_object($r)) {
            $row++;
            echo "<td><table class=\"result\">";
            echo "<tr><td colspan=\"2\" align=\"center\"><a href=\"featured.php?mls=$d->mls\">$d->district</a></td></tr>";
            echo "<tr><td><a href=\"featured.php?mls=$d->mls\"><img border=\"0\" src=\"firstpic.php?mls=$d->mls\" height=\"70\" width=\"100\"></a></td>";
                echo "<td align=\"left\">";
                    setlocale(LC_MONETARY, 'en_US'); //this makes money_format() work
                    echo "<b>Price:</b> ".money_format('%n',$d->asking)."<br />";
                    echo "<b>Type:</b> ".$d->rtype."<br />";
                    echo "<b>Beds:</b> ".$d->bed."<br />";
                    echo "<b>Bath:</b> ".$d->bath."<br />";
                echo "</td>";
            echo "</tr>";
            echo "</table></td>";
            if (($row != 0) && !($row % 3)) {
                echo "</tr>";
            }
        }
        echo "</table>";
    
}
 
All help greatly appreciated.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: MySQL results skipping first row

Post by cpetercarter »

Try incrementing $row at the end of the loop, instead of the beginning?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: MySQL results skipping first row

Post by John Cartwright »

Remove the first

Code: Select all

$d = mysql_fetch_object($r);
You'll notice that you are calling mysql_fetch_object() once outside the loop (thus incrementing the pointer), and a second time in a loop.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: MySQL results skipping first row

Post by iG9 »

@cpetercarter: Yeah I tried that it just resized the table.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: MySQL results skipping first row

Post by iG9 »

@John Cartwright Thanks that did it.
Post Reply