Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

时间:2016-08-31 16:52:12

标签: php html mysql

public function query($sql, $params = array()){
        $this->_error = false;
        if(this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()){
                echo "Success";
            }
        }
    }

I get the following error althought i think all is being done correctly. I pray I get some direction as to what am doing wrong.

1 个答案:

答案 0 :(得分:2)

The issue is on the third line:

if(this->_query

Should be:

if($this->_query

Remember your variables must be prefixed with the dollar sign.

Also, when you see a Parse Error, keep in mind it may have nothing to do with the thing it tells you (the -> for example), rather it is something before that, which makes it unable to parse the statement correctly.