php - 在我的 PHP 代码中找不到 'syntax error'

标签 php database mysqli prepared-statement

我正在编写一个函数来验证用户身份。我创建与数据库的连接,然后准备查询,绑定(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/

相关文章:

php - 如何获取我关注的人的帖子以及我在 php 和 mysql 中的帖子?

PHP - 获取可用数据库名称的下拉列表。

mysql - 在哪里放置外键以获得最佳数据库设计?

PHP - 带有自动增加 ID 的 CSV 导入

php - 使用 php json_encode 形成 json 失败

php - mysql 哪个表更快 : with more rows or columns?

php - 解析 YML 时将变量从 PHP 传递到 YML 文件

javascript - 如何重定向到选择相同选项的不同页面?

mysql - 如果没有事务,wordpress 是如何工作的呢?

php - 如何使用FluentPDO检测错误