Reputation: 77
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
Upvotes: 2
Views: 554
Reputation: 146460
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 orFALSE
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());
Upvotes: 4