PHP PDO::execute语句将不执行

PHP PDO::execute语句将不执行,php,mysql,pdo,execute,Php,Mysql,Pdo,Execute,我试图用PHP创建一个登录/注册代码,将注册数据发送到数据库。我调用了execute()方法,但由于某些原因,它无法工作。应该发生的是,该方法向数据库发送SQL语句,我忽略了什么 public function query($sql, $params= array()) { /* query takes 2 paramaters, $sql which is a prepared sql statement, and params, which holds the value of th

我试图用PHP创建一个登录/注册代码,将注册数据发送到数据库。我调用了execute()方法,但由于某些原因,它无法工作。应该发生的是,该方法向数据库发送SQL语句,我忽略了什么

public function query($sql, $params= array()) {
    /* query takes 2 paramaters, $sql which is a prepared sql statement, and params, which holds the value of the fields to be inserted into db */
    $this->_error = false; // field to determine if there is an error
    $this->_query = $this->_pdo->prepare($sql); // $_query is another field holding the query value

        $x=1;
        if(count($params)){ //count is a function to determine if the paramater has a value
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }
        if($this->_query->execute($sql)){
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount(); // this is the block that is desired to be executed
        } else {
            $this->_error = true; // This is the block that is being executed
        }
    return $this;
}

我验证了连接数据库和query()中的其他方法没有问题根据需要调用的函数

用于检索错误消息或将
PDO::ATTR_ERRMODE
设置为
PDO::ERRMODE_EXCEPTION
,以便PDO在发生错误时抛出异常。

PDO执行参数可能不是查询。在execute上只允许绑定数组。尝试:

public function query($sql, $params= array()) {
    /* query takes 2 paramaters, $sql which is a prepared sql statement, and params, which holds the value of the fields to be inserted into db */
    $this->_error = false; // field to determine if there is an error
    $this->_query = $this->_pdo->prepare($sql); // $_query is another field holding the query value

        $x=1;
        if(count($params)){ //count is a function to determine if the paramater has a value
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }
        if($this->_query->execute()){ // changed execute($sql) to execute()
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount(); // this is the block that is desired to be executed
        } else {
            $this->_error = true; // This is the block that is being executed
        }
    return $this;
}

首先,您必须尝试手动查询, 像

如果这是成功的,那么您可以尝试绑定数据。
我总能找到摆脱错误的方法。

请你的问题澄清一下。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:您能否详细说明“它将不起作用”?为什么要创建一个名为query的函数,而该函数本身已经存在?@CaelanGrgurovic
query
不是保留的术语/字,OP可以使用它,如果它对他们有意义的话。没有造成任何伤害。我知道:)这样做似乎很奇怪……好吧,我运行了它,但我对这种语言不太熟悉,不太明白它的意思->PDO::errorInfo():数组([0]=>21S01[1]=>1136[2]=>列计数与第1行的值计数不匹配这意味着绑定值过多或不足,无法覆盖sql语句中使用的值。
$db = new PDO("mysql:host=somehost;dbname=somedb","user","pass");
$stmt = $db->prepare("insert into table (a,b,c) values('1','2','3')");
$stmt ->execute();