【问题标题】:Undefined property: mysqli_stmt::$bind_param [duplicate]未定义的属性:mysqli_stmt::$bind_param [重复]
【发布时间】:2021-09-16 07:25:33
【问题描述】:

我想通过这种方式使用call_user_func_array绑定变数参数(请看代码处的cmets):


function dbQuery() 
{
    global $dbconnection;
    if (func_num_args() < 1 || func_num_args() === 2) {
       return error('خطای داخلی');
    } else if (func_num_args() === 1) {
       $stmt = $dbconnection->prepare(func_get_arg(0)); // Creating stmt
       if (!$stmt) {
           return error('خطای پایگاه داده');
       }
       if (!$stmt->execute()) {
        return error('خطا در تعامل با پایگاه داده');
       }
       return $stmt;
    } else {
       $args = func_get_args();
       $stmt = $dbconnection->prepare($args[0]); // args[0] is the query with question marks
       if (!$stmt) {
        return error('خطای پایگاه داده');
        }
       array_shift($args);
       foreach ($args as $index => $arg) {
          ${'arg' . $index} = $dbconnection->real_escape_string($arg); // making args as variables because bind_params does not allow direct values
          $args[$index] = ${'arg' . $index};
       }
       if (!call_user_func_array($stmt->bind_param,$args)) { // ERROR IS HERE LINE 67
          return error('خطای امنیتی پایگاه داده');
       }
       if (!$stmt->execute()) {
        return error('خطا در تعامل با پایگاه داده');
       }
       return $stmt;
    }
 }
 $stmt = dbQuery('SELECT * FROM payments WHERE id=?','i',31);
 $result = dbCheck($stmt);
 var_dump($result->fetch_all());

错误是:

PHP Warning:  Undefined property: mysqli_stmt::$bind_param in /home/mwxgaf/w/projects/foodorder/test.php on line 67
PHP Fatal error:  Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given in /home/mwxgaf/w/projects/foodorder/test.php:67
Stack trace:
#0 /home/mwxgaf/w/projects/foodorder/test.php(76): dbQuery()
#1 {main}
  thrown in /home/mwxgaf/w/projects/foodorder/test.php on line 67

【问题讨论】:

标签: php mysql mysqli prepared-statement call-user-func-array


【解决方案1】:

call_user_func_array 需要一个 callable 作为第一个参数,而 $stmt-&gt;bind_param 是错误的格式。 [$stmt, 'bind_param'] 将是正确的可调用对象


来源:https://www.php.net/manual/en/language.types.callable.php

实例化对象的方法作为数组传递,其中包含索引 0 处的对象和索引 1 处的方法名称。允许从类中访问受保护和私有方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-15
    • 2013-12-30
    • 2013-09-13
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多