PHP 只返回 1 行然后复制它
PHP there's only 1 row is returned then duplicates it
我想知道是否有人可以向我解释为什么当 mysql table.
中有 5 个不同的行时它只返回 1 行
它只显示所有 5 行中的第一行,因为我将它放在一个 while 循环中(HTML代码)以便它可以打印所有其他行而不仅仅是第一行。
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>
I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
看看 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
使用相同的旧变量。
解法:
如果要在 table 单元格内显示所有结果集行(包括第一行),则先删除这四行,
$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
我想知道是否有人可以向我解释为什么当 mysql table.
中有 5 个不同的行时它只返回 1 行它只显示所有 5 行中的第一行,因为我将它放在一个 while 循环中(HTML代码)以便它可以打印所有其他行而不仅仅是第一行。
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>
I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
看看 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
使用相同的旧变量。
解法:
如果要在 table 单元格内显示所有结果集行(包括第一行),则先删除这四行,
$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