Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法使用“执行我的PDO查询”;执行();获得;bindParam();错误_Php_Mysql_Sql_Pdo - Fatal编程技术网

Php 无法使用“执行我的PDO查询”;执行();获得;bindParam();错误

Php 无法使用“执行我的PDO查询”;执行();获得;bindParam();错误,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我正在创建一个PDO登录类用于我的项目,但由于我是新手,所以无法将参数绑定到准备好的sql语句。下面是要执行此操作的函数: include_once('connection.php'); class User{ protected $db; public function __construct(){ $oConnection = new Connection; $this->db = $oConnection->getConne

我正在创建一个PDO登录类用于我的项目,但由于我是新手,所以无法将参数绑定到准备好的sql语句。下面是要执行此操作的函数:

include_once('connection.php');

class User{

    protected $db;

    public function __construct(){
        $oConnection = new Connection;
        $this->db = $oConnection->getConnection();
        //var_dump($this->db);
    }

    public function Login($name, $pass){
        if(!empty($name) && !empty($pass)){         
            $st = $this->db;
            $st->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
            $st->bindParam(1, $name);
            $st->bindParam(2, $pass);
            $st->execute();
            var_dump($st);

            if($st->rowCount == 1){
                echo "User verified, Acces granted.";
            }else{
                echo "Incorrect username or password.";
            }

        }else{
            echo "Please fill in the entire form";
        }
    }

}
这里是连接:

class Connection{

    protected $db;

    //Construct
    public function Connection(){

    $conn = NULL;
        try{
            $conn = new PDO("mysql:host=localhost;dbname=<db_name>", "<db_user>", "<db_pass>");
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            } catch(PDOException $e){
                echo 'ERROR: ' . $e->getMessage();
                }

            $this->db = $conn;
    }

    public function getConnection(){
        return $this->db;
    }
}
类连接{
受保护$db;
//构造
公共功能连接(){
$conn=NULL;
试一试{
$conn=new-PDO(“mysql:host=localhost;dbname=”,“”,“”);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
}捕获(PDO$e){
回显“错误:”。$e->getMessage();
}
$this->db=$conn;
}
公共函数getConnection(){
返回$this->db;
}
}
我收到以下错误:

致命错误:调用………中未定义的方法PDO::bindParam()。。。。。。。。。第22行


如果有人能帮我一点忙,那就太好了,我真的很想更好地了解PDo。

您必须捕获prepare调用的结果(这是一个对象),然后在此基础上调用
bindParam()
,而不是PDo对象本身

$st = $this->db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();

$st
现在是PDOStatement对象,您可以调用
bindParam()
execute()
您必须捕获prepare调用的结果(这是一个对象),然后对其调用
bindParam()
,而不是PDO对象本身

$st = $this->db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();

$st
现在是PDOStatement对象,您可以调用
bindParam()
execute()
如果您将MySQL错误复制到google,您将在许多页面中看到相同的错误。 第一条是这样说的:

bindParam()
方法在
PDOStatement
类中,而不是在PDO类中。该语句是prepare()方法的结果


请参阅:

如果您将MySQL错误复制到google,您将在许多页面中看到相同的错误。 第一条是这样说的:

bindParam()
方法在
PDOStatement
类中,而不是在PDO类中。该语句是prepare()方法的结果


请参阅:

您正在使用PDO对象作为要绑定的语句准备对象

$db = $this->db;
$st = $db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();

您正在使用PDO对象作为要绑定的语句准备对象

$db = $this->db;
$st = $db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();