我搜索过并偶然发现了许多像这样的问题。但这没有回答我的问题。因此,我在这里再问一次。这是我的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行的未定义变量行”。任何帮助将不胜感激。
答案 0 :(得分:2)
您需要在while循环中定义$row
以迭代查询中的每条记录:
while($row = mysqli_fetch_array($run)) {
// Do something
}
答案 1 :(得分:1)
您应该定义$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 } } ?>
答案 2 :(得分:0)
只需改变
while($row = mysqli_fetch_array($run)) {
//Code
}
答案 3 :(得分:0)
试试这个
<?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
}
}
?>
答案 4 :(得分:0)
是。因为你没有定义数组$ 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 } } ?>
这应该适合你。
答案 5 :(得分:0)
定义$ row变量。
while($row=mysqli_fetch_array($run))