【问题标题】:PHP there's only 1 row is returned then duplicates itPHP只有1行被返回然后复制它
【发布时间】:2026-01-20 12:10:02
【问题描述】:

我想知道是否有人可以向我解释为什么当 mysql 表中有 5 个不同的行时它只返回 1 行。

它只显示所有 5 行中的第一行,因为我将它放在一个 while 循环 (HTMLcode) 中,以便它可以打印所有其他行,而不仅仅是第一行。

Image that shows the problem

先感谢您 :)

PHP 代码

$id = session_id();
if ($id == "") {
    session_start();
}
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
}

// making the connection to the database
try {
    $host = '127.0.0.1';
    $dbname = 'webdev_2014376';
    $user = 'root';
    $pass = '';
    # MySQL with PDO_MYSQL
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}  

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");   

// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);

// taking each individual piece
$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];

?>



HTML 代码

<?php
     echo 'hello, ' . $_SESSION['username'];
     echo '            ';
     echo $_SESSION['id'];
?>

            <div>   
               <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th class="TableCol1"> Username </th>
                            <th class="TableCol2"> First Name </th>
                            <th class="TableCol3"> Last Name </th>
                            <th class="TableColOpt"> Options </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        while ($check)  {
                            echo '<tr>';
                            echo '<td class="prEach1">' . $username . '</td> ';
                            echo '<td class="prEach1">' . $firstname . '</td> ';
                            echo '<td class="prEach3">' . $lastname . '</td> ';
                            echo '<td class="prEach4 optlinks"> '
                            . '<a href="profile.php?UserID='.$UserID.'">View</a> '
                            . '</td>';
                            echo '</tr>';

                            $check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
                        }
                        ?>
                    </tbody>
                </table> 
            </div>



-------------------------------------------------- -----------------
编辑

我找到了解决方案

SOLUTION IMAGE


PHP 代码

<?php
$id = session_id();
if ($id == "") {
    session_start();
}
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
}

// making the connection to the database
try {
    $host = '127.0.0.1';
    $dbname = 'webdev_2014376';
    $user = 'root';
    $pass = '';
    # MySQL with PDO_MYSQL
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}  

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");

// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);



?>


HTML 代码

<?php
echo 'hello, ' . $_SESSION['username'];
echo '            ';
echo $_SESSION['id'];
?>

<div>   
   <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th class="TableCol1"> Username </th>
                <th class="TableCol2"> First Name </th>
                <th class="TableCol3"> Last Name </th>
                <th class="TableColOpt"> Options </th>
            </tr>
        </thead>
        <!-- This is the category fields on the list. -->
        <tbody>
            <?php
            //$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
            while ($check)  {

                echo '<tr>';
                echo '<td class="prEach1">' . $check['Username'] . '</td> ';
                echo '<td class="prEach1">' . $check['FirstName'] . '</td> ';
                echo '<td class="prEach3">' . $check['LastName'] . '</td> ';
                echo '<td class="prEach4 optlinks"> '
                . '<a href="profile.php?UserID='.$check['UserID'].'">View</a> '
                . '</td>';
                echo '</tr>';

                $check = $sqlQuery->fetch(PDO::FETCH_ASSOC); //THIS SHOULD BE AT THE BOTTOM JUST BEFORE THE WHILE LOOP ENDS
            }
            ?>
        </tbody>
        <!-- This is the get methods of the properties, where the output of the user put in the Property form will be shown -->
    </table> 
</div>

【问题讨论】:

  • 这看起来与您的其他问题 *.com/questions/34557030/php-row-undefined-variable 的下一个完全相同的副本,其中给出了答案但未标记为已解决。那么,这个有什么不同呢?编辑:你犯了同样的错误。
  • 谷歌“php pdo fetch”产生php.net/manual/en/pdostatement.fetch.php,它表示“从结果集中获取下一行”。看看fetchAll;尝试了解正在发生的事情。
  • @Fred-ii- 嗨,是的,我想出了另一个问题,现在我试图找出锄头来显示所有行而不仅仅是一个,但这个只显示第一行当有行时。
  • 那么为什么 Barmar 的答案旁边没有一个绿色的勾号,您没有回复他的最后评论?编辑:好的,你现在标记它。
  • @Fred-ii- 对不起,我是新来的,我不知道你应该那样做,以后会那样做。谢谢

标签: php mysql row


【解决方案1】:

我想知道是否有人可以向我解释为什么当 mysql 表中有 5 个不同的行时它只返回 1 行。

看看while循环内部发生了什么,

while ($check)  {
    echo '<tr>';
    echo '<td class="prEach1">' . $username . '</td> ';
    echo '<td class="prEach1">' . $firstname . '</td> ';
    echo '<td class="prEach3">' . $lastname . '</td> ';
    echo '<td class="prEach4 optlinks"> '
    . '<a href="profile.php?UserID='.$UserID.'">View</a> '
    . '</td>';
    echo '</tr>';

    $check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
}

最初,您使用$row = $sqlQuery-&gt;fetch(PDO::FETCH_ASSOC); 从结果集中仅获取一行并将值放入相应的变量中。在 while 循环中,您正在遍历结果集,但实际上 echoing 使用相同的旧变量。

解决方案:

如果要在表格单元格中显示所有结果集行(包括第一行),则首先删除这四行,

$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);

$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];

然后像这样循环遍历结果集,

// your code

<?php
    while ($row = $sqlQuery->fetch(PDO::FETCH_ASSOC)){
        echo '<tr>';
        echo '<td class="prEach1">' . $row['Username'] . '</td> ';
        echo '<td class="prEach1">' . $row['FirstName'] . '</td> ';
        echo '<td class="prEach3">' . $row['LastName'] . '</td> ';
        echo '<td class="prEach4 optlinks"> '
        . '<a href="profile.php?UserID='.$row['UserID'].'">View</a> '
        . '</td>';
        echo '</tr>';

    }
?>

// your code

【讨论】:

  • 嘿,我也刚刚找到了一个解决方案,我刚刚在那里编辑了我的帖子,谢谢 :) 我们的答案有点相似,但不是你把它作为声明 $row = $sqlQuery-&gt;fetch(PDO::FETCH_ASSOC) 我放了我的在while循环内,就在while循环结束之前:)
  • @MarkyB 我正在写答案,因此没有看到您的编辑。很高兴问题得到解决。干杯! :)