Php MySQLi bind_param()错误

Php MySQLi bind_param()错误,php,mysql,mysqli,Php,Mysql,Mysqli,我试图创建一个接收查询(sql)和参数(数组)的函数,但收到以下错误: PHP警告:mysqli_stmt::bind_param()的参数2应为 给定的参考、值 我的代码是: function dbRead($query, $param) { $mysqli = new mysqli(DB::READ_HOST, DB::READ_USER, DB::READ_PASS, DB::NAME); // Check that connection was successful.

我试图创建一个接收查询(sql)和参数(数组)的函数,但收到以下错误:

PHP警告:mysqli_stmt::bind_param()的参数2应为 给定的参考、值

我的代码是:

function dbRead($query, $param) {
    $mysqli = new mysqli(DB::READ_HOST, DB::READ_USER, DB::READ_PASS, DB::NAME);
    // Check that connection was successful.
    if ($mysqli->connect_error) {
        $result = "Connection error";
    } else {
        // Check that $conn creation succeeded
        if ($conn = $mysqli->prepare($query)) {
            call_user_func_array(array($conn, 'bind_param'), $param);  
            $conn->execute();
            $result = $conn->get_result();
            $result = $result->fetch_array();
            $conn->close();
        } else {
            $result = "Prepare failed";
        }
    }
    $mysqli->close();
    return $result;
}

$test = dbRead('SELECT * FROM user WHERE id=? and email=?', array(123,'example@example.com'))
如果我的功能代码是

function dbRead($query, $param) {
    $mysqli = new mysqli(DB::READ_HOST, DB::READ_USER, DB::READ_PASS, DB::NAME);
    // Check that connection was successful.
    if ($mysqli->connect_error) {
        $result = "Connection error";
    } else {
        // Check that $conn creation succeeded
        if ($conn = $mysqli->prepare($query)) {
            $ref = array();
            foreach ($param as $key => $value) {
                $ref[$key] = &$param[$key];
            }
            call_user_func_array(array($conn, 'bind_param'), $ref);  
            $conn->execute();
            $result = $conn->get_result();
            $result = $result->fetch_array();
            $conn->close();
        } else {
            $result = "Prepare failed";
        }
    }
    $mysqli->close();
    return $result;
}
我收到这个错误

PHP警告:mysqli_stmt::bind_param():类型中的元素数 定义字符串与绑定变量的数量不匹配


我的PHP版本是5.4.36

下面是一些代码,用于绑定“mysqli”查询中存储在数组中的可变数量的参数

我在一个答案中使用了它,这个答案后来被删除了,我希望不是因为我-/

我还没有修改它,因为它符合这些要求,但希望它能显示让“mysqli”查询工作的方法。当时已经测试过了

“棘手”的部分是获得正确的“参考”

<?php   // Q23967579
  // I used a lot of code from this example: http://www.php.net/manual/en/mysqli-stmt.bind-param.php
session_start();

// show input... delete this line later
var_dump($_POST, __FILE__.__LINE__);

// database connection...
$mysqlDb = mysqli_connect('localhost', 'test', 'test', 'testmysql');

$columns = array('category'     => array('type' => 's', 'test' => ' `category` = ? '),
                  'color'       => array('type' => 's', 'test' => ' `color` = ? '),
                  'pic'         => array('type' => 's', 'test' => ' `pic` = ? '),
                  'size'        => array('type' => 's', 'test' => ' `size` = ? '),
                  'min_price'   => array('type' => 'd', 'test' => ' `price` >= ? '),
                  'max_price'   => array('type' => 'd', 'test' => ' `price` <= ? '),
                );

$params = new BindParam();
$whereClause  = '';
$andLit = ' AND ';

// i choose to drive off the search columns...
foreach ($columns as $searchCol => $selection) {
    if (!empty($_POST[$searchCol])) { // process it
        $value = $_POST[$searchCol];
        if ($value == 'all') { // not needed for the search...
            continue;
        }

        // add it to all the various places
        $whereClause .= (!empty($whereClause) ? $andLit : '') . $selection['test'];
        $params->add($selection['type'], $value);
    }
}

// now build the query...
$sql = 'select * from products '. (!empty($whereClause) ? ' where '. $whereClause : '');
$query = mysqli_prepare($mysqlDb, $sql);
$allOk = call_user_func_array('mysqli_stmt_bind_param', array_merge(array($query), $params->get()));
$allOk = mysqli_execute($query);

$queryResult = mysqli_stmt_get_result($query);
while ($row = mysqli_fetch_assoc($queryResult)) {
    var_dump($row);
}
// end of program
exit;

/* -------------------------------------------------
 * utility classes
 */
class BindParam {
    private $values = array(),
             $types = '';

    public function add( $type, $value ) {
        $this->values[] = $value;
        $this->types .= $type;
    }

    public function get() {
        $out = array();
        $out[] = $this->types;
        foreach($this->values as &$value) {
            $out[] =& $value;
       }
       return $out;
   }
}

我试图做一些非常类似的事情,并从PHP参考和
bind_param
上的几个不同帖子中拼凑出解决方案。从bind_param示例(或者您忘记了)中可能无法立即弄清楚的是,第一个参数是参数类型的
字符串,每个参数一个字符(在您的情况下,
很可能是“
表示int和string),您已经知道,其余的参数必须是第二个函数定义中的引用

因此,创建arguments数组应该是这样的:

$ref = array("is");
foreach ($param as $value)
   $ref[count($ref)] = &$value;
虽然有很多方法可以做到这一点。。。您可能应该随查询一起传递参数类型,但是MySQL在类型精确时似乎比较轻松。我还喜欢传递连接,并支持多个结果行,例如:


首先删除
email=“?”
quotes removed@Fred ii-:)中的引号,确定。现在,我不知道你为什么要使用
$conn
,而你已经声明你的连接为
$mysqli=new mysqli
,这可能是导致主要问题的原因。@Fred ii-如果我直接使用
$mysqli
我收到这个错误
调用用户函数数组()期望参数1是一个有效的回调,类“mysqli”没有方法“bind_param”
function doRead($conn, $query, $argTypes, $args){
   $success = false;
   $execParams = array($argTypes);
   foreach($args as $a)
      $execParams[count($execParams)] = &$a;

   if (!$stmt = $conn->prepare($query)){
      echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
   }else if (!call_user_func_array(array($stmt, "bind_param"), $execParams)){
      echo "Param Bind failed, [" . implode(",", $args) . "]:" . $argTypes . " (" . $stmt->errno . ") " . $stmt->error;
   } else if (!$stmt->execute()) {
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
   } else
      $success = true;

   $ret = array();
   if($success){
      $res = $stmt->get_result();
      while ($row = $res->fetch_array(MYSQLI_ASSOC))
         array_push($ret, $row);
   }
   $stmt->close();
   return $ret;
}