0

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>";
        }
    }
}
?>

1 Answer 1

1

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>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@llion Ah yes, I guess I was blinded by frustration and didn't notice how simple it could be :P Thank you :)

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.