首页 > 解决方案 > 解析错误:语法错误,第 29 行 D:\Xampp\htdocs\profile\new\view.php 中的意外 'if' (T_IF)

问题描述

我收到解析错误意外 IF (T_IF)。请帮助我更正我的代码,我在标签之前有 session_start() 并使用会话用户名从数据库中选择行。请帮助我,我的代码无法正常工作

PHP代码

<?php
// connect to the database
include('connect-db.php');
$username = $_SESSION['username']
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM user WHERE username = '$username'"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th></th><th></th></tr>";

while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->firstname . "</td>";
echo "<td>" . $row->lastname . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

标签: phpmysql

解决方案


您收到此错误是因为您缺少一个分号;来终止上述代码的第 4 行,即

$username = $_SESSION['username']

终止行(请参阅添加的分号),问题将得到解决。

$username = $_SESSION['username'];

推荐阅读