我搜索过并偶然发现了许多这样的问题。但这并不能回答我的问题。因此,我在这里再次提出这个问题。这是我的php代码:
<?php
include("connect.php");
if(isset($_GET['view'])) {
$query = "SELECT * FROM posts order by 1 DESC";
$run = mysqli_query($con, $query);
while(mysqli_fetch_array($run)) {
$id = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$content = $row['Post_content'];
?>
<table width="800" align="center" border="5">
<tr>
<td align="center" colspan="9"><h1>View all Posts</h1></td>
</tr>
</table>
<?php
}
}
?>
问题出在最后一行<?php } } ?>
我下面的老师正是这样做的。他的代码运行得非常好。但我的错误是,"第27、28、29、30行的未定义变量行"。如有任何帮助,我们将不胜感激。
您需要在while循环中定义$row
来迭代查询中的每个记录:
while($row = mysqli_fetch_array($run)) {
// Do something
}
您应该定义$row
变量。
<?php
include("connect.php");
if(isset($_GET['view'])) {
$query = "SELECT * FROM posts order by 1 DESC";
$run = mysqli_query($con, $query);
while($row = mysqli_fetch_array($run)) {
$id = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$content = $row['Post_content'];
?>
<table width="800" align="center" border="5">
<tr>
<td align="center" colspan="9"><h1>View all Posts</h1></td>
</tr>
</table>
<?php } } ?>
只需更改
while($row = mysqli_fetch_array($run)) {
//Code
}
尝试这个
<?php
include("connect.php");
if(isset($_GET['view'])) {
$query = "SELECT * FROM posts order by 1 DESC";
$run = mysqli_query($con, $query);
while($row =mysqli_fetch_array($run)) {
$id = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$content = $row['Post_content'];
?>
<table width="800" align="center" border="5">
<tr>
<td align="center" colspan="9"><h1>View all Posts</h1></td>
</tr>
</table>
<?php
}
}
?>
是。因为您没有定义数组$row。你必须定义$row=array();在使用之前。就像下面的
<?php
include("connect.php");
if(isset($_GET['view'])) {
$query = "SELECT * FROM posts order by 1 DESC";
$run = mysqli_query($con, $query);
$row= array();
while($row = mysqli_fetch_array($run)) {
$id = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$content = $row['Post_content'];
?>
<table width="800" align="center" border="5">
<tr>
<td align="center" colspan="9"><h1>View all Posts</h1></td>
</tr>
</table>
<?php } } ?>
这应该对你有用。
定义$row变量。
while($row=mysqli_fetch_array($run))