Jump to content

Archived

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

Prank

Php / Mysql select issue

Recommended Posts

Hi Guys,

I have a table with 5 records in it, when I select the records (even just using SELECT * FROM cars) it will sometimes retrieve 3 records and sometimes 4, never the whole amount. I have never seen this happen before. If I run the query in phpmyadmin it returns all records. I dont understand how this could happen, the Statement could not be more simple.

Anyone know why this could be happening?

Christian

Share this post


Link to post
Share on other sites
Its happening in several places, each with re-written code.

The code couldnt be more simple.

Heres one example;
[code]
        $q = "SELECT * FROM cars";
$qr = mysql_query($q) or die(mysql_error());
$r = mysql_fetch_assoc($qr);

echo '<strong>Select a link category</strong><br />';

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

echo '<a href="links.php?do=listlinks&cat='.$r['name'].'">'.$r['name'].'</a><br />';

} // while[/code]

Thanks.

Share this post


Link to post
Share on other sites
[code]<?php
        $q = "SELECT * FROM cars";
$qr = mysql_query($q) or die(mysql_error());
$r = mysql_fetch_assoc($qr); // <- You already get one row here

echo '<strong>Select a link category</strong><br />';

while($r = mysql_fetch_assoc($qr)){
        // and ofcourse overhere total loops will be minus one of total rows
echo '<a href="links.php?do=listlinks&cat='.$r['name'].'">'.$r['name'].'</a><br />';

} // while
?>[/code]

Share this post


Link to post
Share on other sites
howcome you do a mysql_fetch_assoc, and then do a loop with it again? when you do it the first time, it increments the internal pointer, so that when you start your loop, it starts on the next position, so you're missing out on echoing the first row.

Share this post


Link to post
Share on other sites
Ahh crap, I didnt even pick up on that.

Thanks heaps.

Share this post


Link to post
Share on other sites

×

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.