简单的mysql查询只返回一行

时间:2022-04-12 01:31:45

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)

你可以看到我有2行uid = 1但它只返回1行! (第二个)

$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 个解决方案

#1


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?

你确定在运行的实际代码中,在while循环之前肯定没有$ row = mysql_fetch_assoc($ result)吗?

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.

显然,如果您上面发布的代码正是您正在运行的代码,这不是问题,但这将是此行为的最常见原因。

#2


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?
  • 直接在db中查看确定你确实有两行?

  • 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?
  • 可能是第一条记录是以某种方式自动检索的,所以你实际上是在你的while循环条件下进入第二条记录了吗?

  • What happens if you add a third row? If you get 1 or 2 results, either way it may provide some additional information.
  • 如果添加第三行会发生什么?如果您得到1或2个结果,无论哪种方式,它都可能提供一些额外的信息。

Long shots these, but maybe something will help.

远射这些,但也许会有所帮助。

#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

加。由于uid的类型是INT,你应该写

WHERE uid = 1

instead of

WHERE uid = '1'

#4


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

您是否检查并分析了PHP输出的来源?也许第一行会在HTML中丢失。

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.

正如下面的答案也指出的那样,如果发布的代码正是您正在运行的代码,情况并非如此,但如果那里有一些HTML代码,则可能会发生这种情况。

#1


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?

你确定在运行的实际代码中,在while循环之前肯定没有$ row = mysql_fetch_assoc($ result)吗?

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.

显然,如果您上面发布的代码正是您正在运行的代码,这不是问题,但这将是此行为的最常见原因。

#2


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?
  • 直接在db中查看确定你确实有两行?

  • 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?
  • 可能是第一条记录是以某种方式自动检索的,所以你实际上是在你的while循环条件下进入第二条记录了吗?

  • What happens if you add a third row? If you get 1 or 2 results, either way it may provide some additional information.
  • 如果添加第三行会发生什么?如果您得到1或2个结果,无论哪种方式,它都可能提供一些额外的信息。

Long shots these, but maybe something will help.

远射这些,但也许会有所帮助。

#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

加。由于uid的类型是INT,你应该写

WHERE uid = 1

instead of

WHERE uid = '1'

#4


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

您是否检查并分析了PHP输出的来源?也许第一行会在HTML中丢失。

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.

正如下面的答案也指出的那样,如果发布的代码正是您正在运行的代码,情况并非如此,但如果那里有一些HTML代码,则可能会发生这种情况。