PHP WHILE loop mysteriously not producing any output
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_me开发者_如何学Cssage" 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.
mysql_fetch_array()
doesn't take a query, it takes a resource (result).
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
Also worth nothing that if you are only accessing the associative keys from the result, you should use mysql_fetch_assoc()
instead.
精彩评论