2

Hello I'm trying to query my database table "shout_out", during the while loop I have an if statement to check to if the "convers_id" column has been change from no_reply. if it has I want another query to run and get all the rows from shout_out_reply table that have the same "convers_id" column. I'm trying to get a similar lay out as a facebook "wall". Some one make a comment and others can reply to that comment with the reply being under the original comment.

I keep getting that the mysqli_query($dbc, $query_shout_out_reply) has failed. but there are reply's with the same "convers_id"

$query_shout_out = "SELECT * FROM shout_out WHERE sent_to = '$username' ";
$shout_out_query = mysqli_query($dbc, $query_shout_out);

if (mysqli_num_rows($shout_out_query) != 0) {
    while ($row = mysqli_fetch_array($shout_out_query)) {
        echo '<td class="shout_out_display" width="550">';
        echo $row['message'] . ' <br/><br/> From ' . $row['sent_by'] . ' at ' . $row['date_sent'];
        echo '</td></tr>';
        echo $row['convers_id'];
        if ($row['convers_id'] != "no_reply") {
            $query_shout_out_reply = "SELECT * FROM shout_out_reply WHERE convers_id = " . $row['convers_id'];
            $shout_out_reply_query = mysqli_query($dbc, $query_shout_out_reply);

            while ($reply_row = mysqli_fetch_array($shout_out_reply_query)) {
                echo ' <tr width="550"><td width="125" > &nbsp </td>';
                echo '<td width="425"  class="shout_out_reply_display">';
                echo $reply_row['message'] . '<br/><br/>Reply From ' . $reply_row['sent_by'] . ' at ' . $reply_row['date_sent'];
                echo '</td></tr>';
            }
        }

    }
}
5
  • 2
    I don't have time to answer, but typically running queries in loops is a bad idea. Commented Dec 15, 2011 at 21:44
  • what do you mean failed? No rows returned? Commented Dec 15, 2011 at 21:45
  • I just says Boolean failed when I do a var_dump() Commented Dec 15, 2011 at 21:46
  • do you really need that loop? maybe you can join the tables first then run only one loop - no nested query needed! Commented Dec 15, 2011 at 21:47
  • That would be good but I don't fully understand join, and if I could get this to work I'd rather. Commented Dec 15, 2011 at 21:48

2 Answers 2

1
SELECT * FROM shout_out AS so LEFT JOIN shout_out_reply AS sor ON sor.convers_id = so.convers_id WHERE so.sent_to = '$username' AND so.convers_id != 'no_reply'

The key here is to use an SQL JOIN statement. It'll give you the results from both tables in one statement. ...then you don't have to go through the loop. making all those sql requests.

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

4 Comments

My tables are set up as so sent_by | message | sent_to date_sent | ip | convers_id | reply and the convers_id is the FK in shout_out_reply and the Primary is convers_id in the shout_out, how would i differeniate between the two?
...by using the aliases defined in the statement I supplied. But, are they not the same values? If not, you can do this: SELECT so.convers_id AS so_con, sor.convers_id as sor_con FROM... ...or am I misunderstanding you?
I think this is on the write track but I need to get multiple "comments" like OG post then replies then start over with the next OG post and however many replies
So, you are wanting to group the results? ...so that you have the original post, then all of the replies to that post, then the next original post, then all of the replies to that one, etc?
0

you mast design tables such structure, that will easy you create joins and complex querys. first of all, you need to understood how works joins and when they must use. check google for this.

Comments

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.