PHP & MySQL query value question.
Posted
by space
on Stack Overflow
See other posts from Stack Overflow
or by space
Published on 2010-05-28T04:15:07Z
Indexed on
2010/05/28
4:21 UTC
Read the original article
Hit count: 160
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>';
}
© Stack Overflow or respective owner