You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SELECT查询获取并展示指定UID的所有行数据?

Fix: PHP Query Only Shows One Row & Display Results in HTML Table

Hey there! Let's get your code working to show all UID=1 records in an HTML table. Right now, two main issues are causing only one row to display—let's break them down and fix them step by step:

1. Variable Name Mismatch

In your code, you store the user ID in $uid (from $_SESSION['login']), but try to bind $Uid (uppercase U) to the prepared statement. PHP is case-sensitive, so this will fail to bind the correct value. We need to fix that variable name to match.

2. Single Fetch Call

You only call $result2->fetch_assoc() once, which only grabs the first row of your result set. To get all rows, you need to loop through the result set with a while loop—this will keep fetching rows until there are no more left.


Corrected Full Code

Here's the updated code that fixes both issues and outputs the results in an HTML table:

<?php
// Make sure session is started before accessing $_SESSION
session_start();
$uid = $_SESSION['login'];

// Adjusted ORDER BY to sort by day/time (more meaningful than UID for single user)
$sql2 = "SELECT * FROM Activities WHERE uid=? ORDER BY day, time LIMIT 6";
$stmt2 = $connection->prepare($sql2);
// Bind the correct variable ($uid, not $Uid)
$stmt2->bind_param('i', $uid);
$stmt2->execute();
$result2 = $stmt2->get_result();
?>

<!-- HTML Table for Displaying Results -->
<table border="1" cellpadding="8" cellspacing="0">
    <thead>
        <tr>
            <th>UID</th>
            <th>Day</th>
            <th>Activity</th>
            <th>Time</th>
            <th>Remarks</th>
        </tr>
    </thead>
    <tbody>
        <?php
        // Loop through every row in the result set
        while ($row2 = $result2->fetch_assoc()) {
        ?>
        <tr>
            <td><?php echo htmlspecialchars($row2['Uid']); ?></td>
            <td><?php echo htmlspecialchars($row2['day']); ?></td>
            <td><?php echo htmlspecialchars($row2['activity']); ?></td>
            <td><?php echo htmlspecialchars($row2['time']); ?></td>
            <td><?php echo htmlspecialchars($row2['remarks']); ?></td>
        </tr>
        <?php
        }
        // Handle case where no results are found
        if ($result2->num_rows === 0) {
        ?>
        <tr>
            <td colspan="5">No activities found for this user.</td>
        </tr>
        <?php
        }
        ?>
    </tbody>
</table>

<?php
// Clean up database resources
$stmt2->close();
?>

Key Improvements Explained:

  • session_start(): Added at the top (critical to access $_SESSION before any output is sent to the browser).
  • Fixed variable binding: Matched $uid across the code to ensure the correct user ID is passed to the query.
  • Meaningful sorting: Changed ORDER BY Uid to ORDER BY day, time—since you're filtering for a single UID, sorting by date/time makes results easier to read.
  • while loop: Iterates over every row returned, so all 4 (or up to 6) records will display.
  • htmlspecialchars(): Escapes special characters to prevent XSS attacks—always a safe practice when displaying user-generated data.
  • No-results fallback: Shows a friendly message if there are no activities for the user.

内容的提问来源于stack exchange,提问作者Dellyz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:32:51