0

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
  • 1
    You're missing a quote mark after the SQL on line 1. Commented May 18, 2011 at 3:21

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In his code, $query means $result as it's named incorrectly :)
He used mysql_fetch_array($query) with $query = mysql_query("<statement>"), so $query is a result resource, not a query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.