简体   繁体   中英

Read and display multiple results from a query into a php generated table

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM