tags:

views:

348

answers:

4

I have the following table:

CREATE TABLE IF NOT EXISTS `notes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned NOT NULL DEFAULT '0',
`note` text,
PRIMARY KEY (`id`)
)

INSERT INTO `notes` (`id`, `uid`, `note`) VALUES
(1, 1, 'noteteeext'),
(2, 1, 'notenotenotenote');

As you can see i have 2 rows with uid=1 but it only returns 1 row! (the second one)

$sql = "SELECT id,uid,note
                    FROM notes
      WHERE uid = 1";
$result = mysql_query($sql);


while ($row = mysql_fetch_assoc($result)) {
 echo $row['note'];
}

What's wrong? :/

+5  A: 

Not seeing anything obvious, so I'll go with the simple stuff:

Have you:

  • Looked in the db directly to determine that you do actually have two rows?
  • Verified that those two rows have the data you expect, in the columns you expect?
  • Could it be that the first record is automatically retrieved in some way, so you are actually going to the second record on your while loop condition?
  • What happens if you add a third row? If you get 1 or 2 results, either way it may provide some additional information.

Long shots these, but maybe something will help.

Beska
"What happens if you add a third row?" that's one of the first things i'd try
gradbot
yes, yes, no, i added a third row and yes now it shows 2of3
Ooh...I'm liking something like option 3 then. Somehow you're starting on the first row, and skipping past it in your loop. Have you checked whether $result has something in it before you start your while loop?
Beska
+3  A: 

Are you sure you haven't just miss-looked because it's all written to one line since you're not adding any line break?

Plus. Since the type of uid is INT, you should write

WHERE uid = 1

instead of

WHERE uid = '1'
tharkun
Ooh...good one. I *hate* it when I do something silly like that.
Beska
A: 

Have you checked and analyzed the source of the PHP output? Maybe the first row gets lost in HTML somewhere.

As the answer below also states, this is not the case if the posted code is exactly the code you are running, but if there is some HTML code as well in there, then this might happen.

Gert
+5  A: 

Are you sure there definitely isn't a $row = mysql_fetch_assoc($result) prior to the while loop in the actualy code you are running?

Obviously this is not the problem if the code you have posted above is exactly what you are running, but this would be the most common cause of this behaviour.

William
just checked the code one more time and guess what yore right..sorry for wasting everyones time. lol
good thinking there, William.
tharkun
Well done! ALSO ... this illustrates why you trouble shoot outside of PHP if you can, for example phpMyAdmin or MySQL Query Browser.
Smandoli