2

I recently tried using an old code I wrote for PHP 5.3 on a project that's running on a server that runs PHP 5.6. Though my code runs on my local machine (windows - PHP 5.3), it shows the error below when i try to use on my online host:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /home/lj7mxu21cx3y/public_html/crud/datamodel.php on line 54

Fatal error: Call to a member function execute() on boolean in /home/lj7mxu21cx3y/public_html/crud/datamodel.php on line 55

this is my code below:

function useRecord($mysqli,$query_string="",$type="",$vars=[]){

$query = $mysqli->prepare($query_string);
// create an empty array
$parameters = array();
// push the type string into the array by reference
$parameters[] = & $type;
// push the items from $vars array into the array by reference
for ($i = 0; $i < count($vars); $i++) {
    $parameters[] = & $vars[$i];
}

// call mysqli_stmt::bind_param with the $parameters array, which contains [type, var1, var2, ...]
call_user_func_array(array($query, "bind_param"), $parameters);
$query->execute();
$result = null;
preg_match("/^[A-Z]+/", $query_string, $command);
switch ($command[0]) {
    case "SELECT":
        $result = $query->get_result();
        break;
    case "INSERT":
    case "UPDATE":
    case "DELETE":
        $result = $query->affected_rows;
        break;
}
$query->close();
return $result;



 }

i am using the mysql native driver for php -- mysqlnd on my WebHost (Godaddy). EDIT:: Also, this function is used within a namespaced class

3
  • Could you please run - var_dump($query); and show here. Commented Dec 5, 2019 at 18:00
  • And show how execute this function - useRecord Commented Dec 5, 2019 at 18:06
  • sorry for late response, the question was a little slow to get responses. $query returns a valid statement object, or at least much like this object(mysqli_stmt)#3 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } object(mysqli_stmt)#3 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(4) ["errno"]=> int(0) Commented Dec 5, 2019 at 21:55

1 Answer 1

4

If $query is not an object you need to scroll up to its definition:

$query = $mysqli->prepare($query_string);

Checking documentation we can see the function can return two data types (emphasis mine):

mysqli_prepare() returns a statement object or FALSE if an error occurred.

Your code doesn't handle error conditions. You need to at least detect them:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

A shorter test case (demo):

call_user_func_array(array(false, 'whatever'), array());
Sign up to request clarification or add additional context in comments.

3 Comments

$query returns a valid statement object
@Ndydaniel If that's the case, are you positively sure the code you've shared is exactly the code that exhibits the issue? Is there a chance you're calling the function more than once and only some calls fail? Can you edit the question and show the exact method you've used to determine that $query is a valid statement object?
failure to use this mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); cause the error message to be rather ambiguous. it help me locate a wayward column that wasn't defined on my db. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.