简体   繁体   English

从查询中读取并显示多个结果到php生成的表中

[英]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. 我需要遍历其中一个表中与我存储在会话变量中的用户ID匹配的所有条目。 After that I need to pull information from two of the columns and display them in a php-generated table. 之后,我需要从两列中提取信息,并将其显示在php生成的表中。 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> 我需要将两列中的每一个都放在它们自己的<td>标记中,并且需要将它们都包含在<tr></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. 它会正确显示ID,但我不确定如何显示ID。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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