嵌套 while 循环跳过第一个结果
我需要查询 MySQL 表以获取名称列表,然后根据该名称列表查询与其关联的报告。这是我的代码:
//query the peoples
$query_people = mysql_query("SELECT * FROM people ORDER BY people_name ASC")
while($fetch_people = mysql_fetch_array($query_people)){
$people_id = $fetch_people[people_id];
$people_name = $fetch_people[people_name];
$query_report = mysql_query("SELECT * FROM report WHERE report_entity = '$people_name'");
// output each person's name
echo($people_id.$people_name);
//get their reports
while($fetch_report = mysql_fetch_array($query_report)){
$report_id = $fetch_report[report_id];
$report_type = $fetch_report[report_type];
$report_narr = $fetch_report[report_narr];
echo($report_narr);
}
}
?>
当它输出时,我得到:
1Bill
2Bob 《比尔的叙述》
3汤姆 《鲍勃的叙述》
4 “汤姆的叙述”
关于为什么它跳过比尔对嵌套循环的查询有什么想法吗?
I need to query an MySQL table to get a list of names then, based off that list of names, query reports tied to it. Here is my code:
//query the peoples
$query_people = mysql_query("SELECT * FROM people ORDER BY people_name ASC")
while($fetch_people = mysql_fetch_array($query_people)){
$people_id = $fetch_people[people_id];
$people_name = $fetch_people[people_name];
$query_report = mysql_query("SELECT * FROM report WHERE report_entity = '$people_name'");
// output each person's name
echo($people_id.$people_name);
//get their reports
while($fetch_report = mysql_fetch_array($query_report)){
$report_id = $fetch_report[report_id];
$report_type = $fetch_report[report_type];
$report_narr = $fetch_report[report_narr];
echo($report_narr);
}
}
?>
When it outputs, I get this:
1Bill
2Bob
"Bill's narrative"
3Tom
"Bob's narrative"
4
"Tom's narrative"
Any thoughts on why it is skipping Bill's query on the nested loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于某种原因,我无法评论您的问题,所以我将发布答案。看来您遇到了某种离一索引问题。
正如您所看到的,“比尔的叙述”正在与鲍勃一起打印,“鲍勃的叙述”正在与汤姆一起打印。另外,您没有与 Bill 进行叙述,并且只有索引 4 的叙述。但是,由于您实际上并未使用索引来执行任何查询,因此可能是以下两种情况之一:
1)表中的数据不是“ t 正确存储。
2)您正在执行的查询没有返回您期望的结果。
对于这两种情况,请尝试运行您在 mysql 代码中发布的确切查询,并查看数据是否符合您的预期。另外,尝试添加更多 echo 语句,例如
$people_id
和$people_name
。并回显创建$query_report
的第二条 sql 语句,确保这就是您所期望的。最后,通过打印始终显示的内容或向现有 echo 语句添加引号,确保您进入嵌套的 while 循环:
这样您就知道您至少进入了循环并且查询返回了结果。否则,您的数据库可能不会按照您想象的方式排列。
For some reason I can't comment on your question, so I will post an answer instead. It looks like you have some sort of off-by-one index problem.
As you see "Bill's narrative" is being printed with Bob and "Bob's narrative" is being printed with Tom. Also you have no narrative with Bill and you only have a narrative with index 4. However, since you don't really use indexes to do any of your queries, it may be one of two things:
1) Data in the table isn't stored correctly.
2) The queries you are doing aren't returning what you expect.
For both, try running the exact queries you posted in your code in mysql and see if the data is what you expect. Also, try adding more echo statements like what are
$people_id
and$people_name
. And echo the second sql statement that creates$query_report
, make sure that is what you expect it to be.Finally, make sure that you are entering the nested while loop by printing something that will always show up or adding quotes to your existing echo statement:
This way you know you at least entered the loop and the query returned results. Otherwise, your database may not be arranged the way you think it is.
据我了解,您无法保证 mysql 语句在事务之外执行的顺序。我会使用 join 来代替,它会比内部带有 mysql 查询的嵌套循环更快。
From what I understand you can't guarantee the order mysql statements execute outside of transactions. I would use a join instead, it will be quicker than nested loops with mysql queries inside of them.
好吧,我对此没有任何解释 - 我从头开始并且成功了。感谢大家的帮助!
Well, I have no explanation for this - I started from scratch and it worked. Thanks all for your help!