Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.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 BindParam()工作不正常_Php_Pdo_Bindparam - Fatal编程技术网

Php BindParam()工作不正常

Php BindParam()工作不正常,php,pdo,bindparam,Php,Pdo,Bindparam,在我的DB类中,我有一个方法query(),它将SQL语句和参数作为参数,并在数据库中运行它们 // the parameters are the sql statement and the values, returns the the current object // the result set object can be accessed by using the results() method public function query($sql, $pa

在我的DB类中,我有一个方法
query()
,它将SQL语句和参数作为参数,并在数据库中运行它们

    // the parameters are the sql statement and the values, returns the the current object
    // the result set object can be accessed by using the results() method

    public function query($sql, $params = array()) {

        $this->_error = false;  // initially the error is set to false

        if($this->_query = $this->_pdo->prepare($sql)) {

            // if the parameters are set, bind it with the statement
            if(count($params)) {
                foreach($params as $param => $value) {
                    $this->_query->bindParam($param, $value);
                }
            }

            // if the query is successfully executed save the resultset object to the $_results property
            // and store the total row count in $_count
            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }

        return $this;

    }
这就是我调用方法的方式

$db->query("SELECT * FROM users WHERE username = :username AND id = :id ", array(':username' => 'sayantan94', ':id' => 1));
但是当我打印结果集时,得到的是一个空数组。但如果我这样做

$db->query("SELECT * FROM users WHERE username = :username", array(':username' => 'sayantan94'));
还是这个

$db->query("SELECT * FROM users WHERE id = :id ", array(':id' => 1));

我得到了正确的结果。我的代码有什么问题???

您的foreach是false,$value by value,因为bindParam需要&$variable,请创建一个引用,然后尝试以下操作:

if(count($params)) {
   foreach($params as $param => &$value) {
       $this->_query->bindParam($param, $value);
   }
}

除了在另一个答案中所说的之外,您的大多数代码要么是无用的,要么是有害的。事实上,你只需要这几行

public function query($sql, $params = array())
{
    $query = $this->_pdo->prepare($sql);
    $query->execute($params);
    return $query; 
}
它将完成你函数所做的一切,但是没有那么多代码,也没有令人讨厌的副作用。假设您将在循环中运行嵌套查询。第二次执行查询后,$this->\u results变量会变成什么?在这样的函数中,不应该有与特定查询相关的类变量。

此外,

  • 无需手动检查准备结果-PDO将自己抛出一个异常
  • 不需要
    \u error
    变量,因为它非常无用,而PDO自己的异常更有用,信息量更大
  • 无需在循环中绑定-
    execute()
    可以一次绑定所有参数
  • 无需测试
    $params
    数组-
    execute()
    也将接受空数组
  • 无需返回
    fetchAll()
  • 不需要
    rowCount()
    -您始终可以对从fetchAll()返回的数组进行计数

我收到了这个错误
警告:PDOStatement::bindParam():SQLSTATE[HY093]:无效的参数编号:第50行的D:\xampp\htdocs\oop\classes\DB.php中的列/参数是以1为基础的
@SayantanDas请查看我的文章,以获得对您使用的一些错误实践的深入解释,谢谢。我一定会查那篇文章。