0
votes

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:

  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.

1
You're missing a quote mark after the SQL on line 1. - Rafe Kettler

1 Answers

6
votes

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.