TooFast TooFast - 3 months ago 8
PHP Question

Comment reply select not working good

I have a problem and I don't know how to resolve...
I have page with comments and page with replies.
Down is my code, everything is ok, it shows all I want but the problem is, when I click on the comment to go to the page with a reply, the id it gets is wrong.

Example: for the first comment I need to have id=1, second id=2 etc. But I get id=1 for all the comments where user_id = 1 and not id from comment.
Sorry, my English.

<?php
$id = $show['id'];
$sq = "SELECT * FROM comments, users WHERE comments.user_id = users.id";
$re = mysqli_query($dbCon, $sq);
while($abc=mysqli_fetch_assoc($re)){
?>
<div class="card hoverable q_area">
<div class="card-content">
<div class="chip">
<img src="<?php echo $show['profile_foto'] ?>">
<?php echo $abc['user'] . " said:"; ?>
</div><br />
<div id="comm">
<?php $a=substr(str_replace(' ','-',$abc['question']), 0, 50); ?>
<h5><a href="replys.php?id=<?php echo $abc['id'] ?>&reply=<?php echo $a ?>">
<?php echo $abc['comment']; ?></a></h5> <br />
</div>
</div>
</div>
<?php } ?>





If I change that select like this:

$sq = "SELECT * FROM comments";


then link id work, but I can't echo the user who said the comment.

database tables:
users - id, user, pass
comments- id, user_id, comment


please help me :(

Answer

Change Your query - $sq to this one:

$sq = "SELECT a.*, b.user FROM comments as a, users as b WHERE a.user_id = b.id"