rag
rag

Reputation: 515

PHP mysql_fetch_array

    $n=mysql_num_rows($rs);
    $i=0;    
    while($n>0)
        {
            while(($row=mysql_fetch_array($rs))&&$i<=5)
                {

                    echo $row['room_name'];
                    $i=$i+1;
                    //echo $n."<br>";
                }
                echo "<br>";
        //echo "n1=".$n;
        $n=$n-5;
        //
        $i=0;
        }

Output:101102103104105106
108109110

The row for roomname 107 is missing.... anybody please tell me what is the problem while reentering the loop again...

Upvotes: 0

Views: 843

Answers (3)

Chad Birch
Chad Birch

Reputation: 74558

Just to follow up on my comment, this whole chunk of code could have been written much more clearly as follows. (assuming you meant to put in a <br> after every 5 records, right now you're doing it after 6 but I think that's probably a mistake)

$rownum = 1;
while ($row = mysql_fetch_array($rs))
{
    echo $row['room_name'];

    if ($rownum % 5 == 0)
        echo '<br>';
    $rownum++;
}

Upvotes: 2

sam
sam

Reputation: 376

here if are checking $i<=5 condition so array stats from 0 , so your database values stats from 101,102,..106, so it will 6 elements .

$i<=5 condition remove this condition in while keep the condition if($i%5==0) echo "
"; it will works

Upvotes: 0

codaddict
codaddict

Reputation: 455030

When $i becomes 6 you fetch a row but do nothing. Because fetching happens before the $i<=5 check, the fetched row gets skipped.

Change the order of conditions in the while loop.

while(($row=mysql_fetch_array($rs))&&$i<=5)

To

while($i<=5 && ($row=mysql_fetch_array($rs)))

Upvotes: 2

Related Questions