简单的mysql查询只返回一行

我有下表:</ p>

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

INSERT INTOnotesiduidnote)VALUES
(1,1,'noteteeext'),
(2,1,'notenotenotenote');

</ code> </ pre>

正如您所看到的,我有2行uid = 1但它只返回1行! (第二个)</ p>

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

而($ row = mysql_fetch_assoc($ result)){
echo $ row ['note'];
}
</ code> </ pre>

怎么了? :/ </ p>
</ div>

展开原文

原文

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

donglan9517
donglan9517 :O它返回所有行。嗯
大约 11 年之前 回复
duangang4940
duangang4940 +1...问题解决时,如果可以,请删除软件层!
大约 11 年之前 回复
dongpo2014
dongpo2014 您是否尝试直接在数据库上运行查询(或通过phpMyAdmin)?
大约 11 年之前 回复
download1323
download1323 你在哪里定义$result?你可以加一下吗?mysql_num_rows($result)返回什么?
大约 11 年之前 回复

4个回答



你确定在此之前肯定没有 $ row = mysql_fetch_assoc($ result)</ code> 在你正在运行的实际代码中循环?</ p>

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

展开原文

原文

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.

douxie5176
douxie5176 做得好! 另外......这说明了为什么你麻烦在PHP之外拍摄,如果可以的话,例如phpMyAdmin或MySQL Query Browser。
大约 11 年之前 回复
dongxie3963
dongxie3963 好的想法,威廉。
大约 11 年之前 回复
douji8347
douji8347 刚刚检查了一下代码,然后猜猜你是对的......想浪费每个人的时间。 大声笑
大约 11 年之前 回复



你确定你没有错过,因为你没有添加任何换行符,所有这些都被写入一行吗? </ p>

加。 由于uid的类型是INT,你应该写</ p>

  WHERE uid = 1 
</ code> </ pre>

而不是</ p>

  WHERE uid ='1'
</ code> </ pre>
</ div>

展开原文

原文

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'

douzhang3898
douzhang3898 哦......好的。 当我做那样愚蠢的事情时,我讨厌它。
大约 11 年之前 回复



没有看到任何明显的东西,所以我会选择简单的东西:</ p>

你有没有 :</ p>


  • 直接查看数据库以确定您确实有两行?</ li>
  • 验证这两行是否包含数据 期望,在你期​​望的列中?</ li>
  • 可能是第一条记录是以某种方式自动检索的,所以你实际上是在你的while循环条件下转到第二条记录了吗?</ li>

  • 如果添加第三行会怎样? 如果你得到1或2个结果,无论哪种方式它都可以提供一些额外的信息。</ li>
    </ ul>

    远射这些,但也许会有所帮助。</ p>
    </ DIV>

展开原文

原文

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.

dongyuan9892
dongyuan9892 噢......我喜欢类似选项3的东西。 不知何故,你从第一行开始,并在你的循环中跳过它。 在开始while循环之前,您是否检查过$ result是否包含某些内容?
大约 11 年之前 回复
douzhiba6873
douzhiba6873 是的,是的,不,我添加了第三行,现在它显示了2of3
大约 11 年之前 回复
dongsilu2237
dongsilu2237 “如果添加第三行会怎么样?” 这是我尝试的第一件事之一
大约 11 年之前 回复



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

如下面的答案所述,如果发布的代码完全</ strong>您的代码,情况并非如此 正在运行,但如果那里有一些HTML代码,那么可能会发生这种情况。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问