【发布时间】: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>
-------------------------------------------------- -----------------
编辑
我找到了解决方案
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- 对不起,我是新来的,我不知道你应该那样做,以后会那样做。谢谢