我想知道是否有人可以向我解释为什么当mysql表中有5个不同的行时它只返回1行。
它只显示所有5行中的第1行,因为我将它放在while循环(HTMLcode)中,以便它可以打印所有其他行而不仅仅是第一行。
Image that shows the problem
先感谢您 :)
PHP代码
Features
HTML代码
$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'];
?>
的 ----------------------------------------------- --------
编辑
我找到了解决方案
PHP代码
<?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>
HTML代码
<?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);
?>
答案 0 :(得分:0)
我想知道是否有人可以向我解释为什么当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->fetch(PDO::FETCH_ASSOC);
从结果集中仅提取了一行,并将值放在相应的变量中。在while
循环中,您循环遍历结果集,但实际上echo
使用相同的旧变量。
<强>解决方案:强>
如果要在表格单元格中显示所有结果集行(包括第一行),请先删除这四行,
$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