PHP & MySQL query value question.
- by space
How can I use the first query's id value $row['id'] again after I run a second query inside the while loop statement? To show you what I mean here is a sample code below of what I'm trying to do.
I hope I explained it right.
Here is the code.
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*
FROM users
WHERE user_id = 4");
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
}
while($row = mysqli_fetch_assoc($dbc)) {
echo '<div>User: ' . $row['id'] . '</div>';
echo '<div>Link To User' . $row['id'] . '</div>';
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc2 = mysqli_query($mysqli,"SELECT COUNT(cid) as num
FROM comments
WHERE comments_id = $row[id]");
if (!$dbc2) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc2)){
$num = $row['num'];
}
}
echo '<div>User ' . $row['id'] . ' Comments# ' . $num . '</div>';
}