Search code examples
phpmysqlmysqliprepared-statementcall-user-func-array

Undefined property: mysqli_stmt::$bind_param


I want to bind variable number parameters using call_user_func_array in this way(please read comments at the code):


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());

and the error is:

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


Solution

  • call_user_func_array expects a callable as the first argument, and $stmt->bind_param is the wrong format for this. [$stmt, 'bind_param'] would be the proper callable


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

    A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.