PHP WHILE 循环神秘地不产生任何输出
首先是代码,然后是解释:
$result = mysql_query("INSERT INTO messages (...) VALUES (...)") or die(mysql_error());
$rowID = mysql_insert_id();
if($result) {
$query = mysql_query("
SELECT ... LIMIT 1");
while ($row = mysql_fetch_array($query)) :
$message_id = stripslashes($row["m_id"]);
$message_postedby = stripslashes($row["u_name"]);
$message_text = stripslashes($row["m_text"]);
$date = date('F d, Y \a\t g:iA', strtotime($row["m_date"]));
?>
<div class="wall_message" id="<?= $message_id ?>">
<div class="author"><?= $message_postedby ?></div>
<div class="message"><?= $message_text ?></div>
<div class="date"><?= $date ?></div>
</div>
<?php
endwhile;
} ?>
我正在做什么:
- 将新行插入数据库
- 将插入行的 ID 存储在变量 $rowID 中
- 如果插入成功,则查询数据库以检索该行以及其他表中的任何其他相关信息
- 存储来自的值变量中的行
- 打印包含该信息的 div
所有这些都是从 jQuery 函数调用的,因此我期望所有 HTML 都将被返回,但 WHILE 循环内没有返回任何内容。我可以在它之前和之后回显文本,并且我看到它很好,但是循环内不会打印任何内容,而且我只是看不到我的错误。也没有收到任何错误。
谢谢。
Code first, explanation after:
$result = mysql_query("INSERT INTO messages (...) VALUES (...)") or die(mysql_error());
$rowID = mysql_insert_id();
if($result) {
$query = mysql_query("
SELECT ... LIMIT 1");
while ($row = mysql_fetch_array($query)) :
$message_id = stripslashes($row["m_id"]);
$message_postedby = stripslashes($row["u_name"]);
$message_text = stripslashes($row["m_text"]);
$date = date('F d, Y \a\t g:iA', strtotime($row["m_date"]));
?>
<div class="wall_message" id="<?= $message_id ?>">
<div class="author"><?= $message_postedby ?></div>
<div class="message"><?= $message_text ?></div>
<div class="date"><?= $date ?></div>
</div>
<?php
endwhile;
} ?>
What I'm doing:
- Insert new row into database
- Store ID of inserted row in variable $rowID
- If insert was successful, query the database to retrieve that row along with any other related information from other tables
- Store values from the row in variables
- Print the divs containing that info
All of this is being called from a jQuery function, so I'm expecting that all of my HTML will be returned, but nothing is being returned inside the WHILE loop. I can echo text before and after it, and I see it just fine, but nothing inside the loop will print, and I just can't see my mistake. Not getting any errors either.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mysql_fetch_array()
不接受查询,它接受资源(结果)。同样值得一提的是,如果您仅从结果中访问关联键,则应该使用 mysql_fetch_assoc() 来代替。
mysql_fetch_array()
doesn't take a query, it takes a resource (result).Also worth nothing that if you are only accessing the associative keys from the result, you should use
mysql_fetch_assoc()
instead.