我正在编写一个函数来验证用户身份。我创建与数据库的连接,然后准备查询,绑定(bind)参数,执行查询,将结果绑定(bind)到变量,检查查询是否返回结果。
如果是,我会比较结果(绑定(bind)到变量),关闭语句,关闭连接,然后返回适当的值。好吧,这就是我认为我正在做的事情,但是我不断收到语法错误,并且我无法弄清楚我做错了什么:
Syntax error: expected: exit, if, identifier, variable, echo, do, while, for, foreach, declare, switch, break, continue, function, return, try, throw, use, global, unset, isset, empty, class, interface, array, {, }, include, include_once, eval, require, require_once, print, ';', +, -, !, ~, ++, --, @, [, new, static, abstract, final, (, $
我的代码:
/**
* Authenticates a user.
* @param type $email - String value
* @param type $hashedPassword - String value
* @return true if user is authenticated or false otherwise - Boolean value
*/
function isValidUser($email, $hashedPassword)
{
//This variable will hold the value returned from the query to the database.
var $rPassword = NULL;
//Establish a connection
$mysqli = new mysqli($GLOBALS['dbServer'], $GLOBALS['dbUserName'], $GLOBALS['dbPassword'], $GLOBALS['dbName']);
//Check if connection failed
if($mysqli->connect_error)
{
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$stmt = $mysqli->prepare("SELECT password FROM user_info WHERE email=?");
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->bind_result($rPassword);
if($stmt->fetch())
{
if(($rPassword != null) && ($rPassword == $hashedPassword))
{
$stmt->close();
$mysqli->close();
return true;
}
}
$stmt->close();
$mysqli->close();
return false;
}
我在没有使用准备好的语句的情况下执行此操作,并且代码运行良好,但后来我做了一些研究,发现准备好的语句是正确的方法,因为它们有助于防止 SQL 注入(inject)。
最佳答案
var $rPassword = NULL;
应该是:
$rPassword = NULL;
var
用于初始化类中的属性。请参阅documentation 。如果您使用的是类,则需要在方法(函数)之外对其进行初始化,然后通过 $this->rPassword
访问该属性。
关于php - 在我的 PHP 代码中找不到 'syntax error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14552938/