解析错误:语法错误,意外' $ user'第4行的C:\ xampp \ htdocs \ home \ login.php中的(T_VARIABLE)
<?php
$host = "localhost"
$user = "root"; <------ line 4
$pass = "";
$db = "table";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$res = mysql_query($sql);
if (mysql_num_rows($res) == 1) {
echo "Super";
exit();
} else {
echo "Siper";
exit();
}
}
?>
如何解决这个问题?
答案 0 :(得分:1)
缺少分号:
<?php
$host = "localhost"; <--- error :D
$user = "root"; <------ line 4
$pass = "";
$db = "table";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$res = mysql_query($sql);
if (mysql_num_rows($res) == 1) {
echo "Super";
exit();
} else {
echo "Siper";
exit();
}
}
?>
答案 1 :(得分:1)
你在第一行丢失了一个分号。请记住,在php中,错误通常是指周围所指定的行号,而不一定是 行。 :)
即。
$host = "localhost";
编辑:
您可能还需要清理$ _POST数据,或者重新考虑构建此数据的方式,否则您将对SQL注入保持开放状态。
请阅读此处了解更多信息: What's the best method for sanitizing user input with PHP?