2

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? :/

4
  • Where are you defining $result? Could you add that please? What does mysql_num_rows($result) return? Commented Jul 21, 2009 at 21:28
  • 4
    Have you tried running the query directly on the database (or through phpMyAdmin)? Commented Jul 21, 2009 at 21:29
  • +1 ...When trouble shooting, remove a software layer if you can! Commented Jul 21, 2009 at 21:34
  • @hobodave :O it returns all rows. hmmm Commented Jul 21, 2009 at 21:41

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

2 Comments

"What happens if you add a third row?" that's one of the first things i'd try
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?
3

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'

1 Comment

Ooh...good one. I hate it when I do something silly like that.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.