简体   繁体   English

(PHP)警告:mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配

[英](PHP) Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

I have the following query. 我有以下查询。

    $mysqldb = mysqlidb_class();
    $query = "select * from post where idx < ?"

Then I bind the parameter and execute. 然后绑定参数并执行。

    $bindvariable = array();
    array_push($bindvariable, $post_photoidx);
    array_push($bindvariable, $post_idx);
    $res = $mysqldb->rawQuery($query, $bindvariable);

Then I get the following error. 然后我得到以下错误。

    Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

But when I change the query like below, the error disappears. 但是,当我如下更改查询时,错误消失了。

    $query = "select * from post where idx = ?"

What am I doing wrong here? 我在这里做错了什么?

Here is the class I use for the mysql query 这是我用于mysql查询的类

    <?php

    class MysqliDb
    {
        ......

        public function rawQuery ($query, $bindParams = null, $sanitize = true)
        {
            $this->_query = $query;
            if ($sanitize)
                $this->_query = filter_var ($query, FILTER_SANITIZE_STRING,
                                        FILTER_FLAG_NO_ENCODE_QUOTES);
            $stmt = $this->_prepareQuery();
            if (is_array($bindParams) === true) {
                $params = array(''); // Create the empty 0 index
                foreach ($bindParams as $prop => $val) {
                    $params[0] .= $this->_determineType($val);
                    array_push($params, $bindParams[$prop]);
                }
                call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
            }
            $stmt->execute();
            $this->_stmtError = $stmt->error;
            $this->reset();
            return $this->_dynamicBindResults($stmt);
        }

        ......

        protected function _buildQuery($numRows = null, $tableData = null)
        {
            $this->_buildJoin();
            $this->_buildTableData ($tableData);
            $this->_buildWhere();
            $this->_buildGroupBy();
            $this->_buildOrderBy();
            $this->_buildLimit ($numRows);
            $this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams);
            if ($this->isSubQuery)
                return;
            // Prepare query
            $stmt = $this->_prepareQuery();
            // Bind parameters to statement if any
            if (count ($this->_bindParams) > 1)
                call_user_func_array(array($stmt, 'bind_param'), $this->refValues($this->_bindParams));
            return $stmt;
        }

        protected function _prepareQuery()
        {
            if (!$stmt = $this->_mysqli->prepare($this->_query)) {
                trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR);
            }
            return $stmt;
        }

        protected function refValues($arr)
        {
            //Reference is required for PHP 5.3+
            if (strnatcmp(phpversion(), '5.3') >= 0) {
                $refs = array();
                foreach ($arr as $key => $value) {
                    $refs[$key] = & $arr[$key];
                }
                return $refs;
            }
            return $arr;
        }

    ......

    } // END class

You mightn't use array(2). 您可能不使用array(2)。 Instead, use 相反,使用

$sql = "select * from post where idx < :i";
$stmt->bindparam("i", 2);
$stmt->execute();

or use 或使用

$array = array($something,$else,$whatever); 
$sql = "select * from post where idx < ?";
$stmt->bindparam("i", $array[2]);
$stmt->execute();

It looks like you are not preparing your query before biding parameters to it. 似乎您没有在为参数出价之前准备查询。

$sql = "SELECT * FROM post WHERE idx < ?"; 

if($stmt = $stmt->prepare($sql)) {
    $stmt->bind_param('i', 2);
    $stmt->execute();
}   

Santize filters ruined my SQL query. Santize过滤器破坏了我的SQL查询。

I have changed the source to the following to resolve the problem. 我已将源更改为以下内容以解决此问题。

    $mysqldb->rawQuery($query, $bindvariable, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:mysqli_stmt :: bind_param()变量数与准备好的语句中的参数数不匹配 - Warning: mysqli_stmt::bind_param() Number of variables doesn't match number of parameters in prepared statement mysqli_stmt :: bind_param():变量数量与php中准备好的语句中的参数数量不匹配 - mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in php PHP 警告:mysqli_stmt::bind_param():变量数与准备好的语句中的参数数不匹配 - PHP Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Mysqli:mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配 - Mysqli:mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配 - mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement mysqli_stmt :: bind_param变量数与准备好的语句中的参数数不匹配 - mysqli_stmt::bind_param Number of variables doesn't match number of parameters in prepared statement 警告:mysqli_stmt::bind_param():变量数与 C:\User..\ on 148 中准备好的语句中的参数数不匹配 - Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\User..\ on 148 mysqli_stmt :: bind_param():第64行上已准备好的语句中的变量数量与参数数量不匹配 - mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement on line 64 call_user_func_array()-警告:mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配 - call_user_func_array() - Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement mysqli_stmt::bind_param() [mysqli-stmt.bind-param]:变量数量与参数数量不匹配 - mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM