PHP 关闭 while 循环时在关闭标记外并使用 Html 时出错
PHP errors when closing while loop outside closing tag and using Html alongside
我搜索并偶然发现了很多这样的问题。但这并没有回答我的问题。因此,我在这里再次询问。这是我的 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 } } ?>
我跟随的讲师就是这样做的。他的代码运行良好。但是我的报错,"Undefined variable row at line 27, 28, 29, 30"。任何帮助将不胜感激。
您需要在 while 循环中定义 $row
以迭代查询中的每条记录:
while($row = mysqli_fetch_array($run)) {
// Do something
}
只是改变
while($row = mysqli_fetch_array($run)) {
//Code
}
您应该定义 $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 } } ?>
试试这个
<?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。你必须定义
$行=数组();在使用它之前。如下所示
<?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))
我搜索并偶然发现了很多这样的问题。但这并没有回答我的问题。因此,我在这里再次询问。这是我的 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 } } ?>
我跟随的讲师就是这样做的。他的代码运行良好。但是我的报错,"Undefined variable row at line 27, 28, 29, 30"。任何帮助将不胜感激。
您需要在 while 循环中定义 $row
以迭代查询中的每条记录:
while($row = mysqli_fetch_array($run)) {
// Do something
}
只是改变
while($row = mysqli_fetch_array($run)) {
//Code
}
您应该定义 $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 } } ?>
试试这个
<?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。你必须定义 $行=数组();在使用它之前。如下所示
<?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))