Reputation: 876
I need to loop through all of the entries in one of my tables that match a user id which I've stored in a session variable. After that I need to pull information from two of the columns and display them in a php-generated table. I need each of the two columns to be in their own <td>
tags and I need them both to be encompassed inbetween <tr>
and </tr>
So far I've written the code to find all the information and now I'm having trouble displaying it. It will display the id correctly but I'm unsure of how to get it to display the username as well.
Here's the code I have as of now:
<?php
$id = $_SESSION['uid'];
$query1 = mysql_query("SELECT ID FROM users WHERE id=$id");
if(mysql_num_rows($query1) == 0) {
echo "<tr><td>Error!</td><td>No matching information.<td></tr>";
} else {
while($queryarray = mysql_fetch_array($query1, MYSQL_ASSOC)){
foreach ($querryarray as $idnum) {
$query2 = mysql_query("SELECT name FROM users WHERE ID=$query1");
echo "<tr><td>$idnum</td><td>???</td></tr>";
}
}
}
?>
Upvotes: 0
Views: 471
Reputation: 6872
You can do it in one query and then access the associative array.
<?php
$id = $_SESSION['uid'];
$query1 = mysql_query("SELECT id,name FROM users WHERE id=$id");
if(mysql_num_rows($query1) == 0) {
echo "<tr><td>Error!</td><td>No matching information.<td></tr>";
} else {
while($queryarray = mysql_fetch_array($query1, MYSQL_ASSOC)){
echo "<tr><td>{$queryarray['id']}</td><td>{$queryarray['name']}</td></tr>";
}
}
?>
Upvotes: 1