Rippan
Rippan

Reputation:

Simple mysql query only returning one row

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

Upvotes: 2

Views: 5715

Answers (4)

Gert
Gert

Reputation: 200

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.

Upvotes: 0

William
William

Reputation: 13632

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.

Upvotes: 6

markus
markus

Reputation: 40675

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'

Upvotes: 3

Beska
Beska

Reputation: 12667

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.

Upvotes: 6

Related Questions