开发者

PHP WHILE loop mysteriously not producing any output

开发者 https://www.devze.com 2023-03-06 11:40 出处:网络
Code first, explanation after: $result = mysql_query(\"INSERT INTO messages (...) VALUES (...)\") or die(mysql_error());

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:

  1. Insert new row into database
  2. Store ID of inserted row in variable $rowID
  3. If insert was successful, query the database to retrieve that row along with any other related information from other tables
  4. Store values from the row in variables
  5. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消