0

I have my app build in localhost with MySQL prepare statement as following:

include('db.php');

$stmt = $mysqli->prepare("SELECT * FROM `hero` WHERE name=? LIMIT 1") or die($mysqli->error);

$stmt->bind_param('s', $name);

$name = 'superman';

$stmt->execute();

$check = $stmt->get_result();
$rows = $check->fetch_object();

$num = $check->num_rows;

if($num == 0){
    echo 'Failed!';
}else{
    echo 'Found!';
}

The query is working fine in localhost, when I tested in web hosting there is an error shown:

Fatal error: Call to undefined method mysqli_stmt::get_result()...

After some web research, the catch was it was caused by mysqld driver not being installed in hosting (PHP version 5.4++, I reckon 5.3 onwards are already available?).

Is there any workaround to using an alternate way instead of mysqli_stmt::get_result?

halfer
  • 19,824
  • 17
  • 99
  • 186
conmen
  • 2,377
  • 18
  • 68
  • 98

2 Answers2

0

I had your very same problem. you can either contact your hosting provider or find a workaround for this issue..

mysqlnd is in the source of PHP 5.3 but not compiled with all distributions, it's the default for 5.4+

suggested workaround: you could try mysqli_stmt_bind_result or totally replace mysqli prepared with PDO

Ehab Eldeeb
  • 722
  • 4
  • 12
0

(Caveat: I'm using PHP 7.x so this may or may not work for you!)

I know there has been talk about "mysqli_stmt_get_result" not working. I have experienced that problem! Refer to this link: PHP Fatal error: Call to undefined function mysqli_stmt_get_result()

(see Jeff's answer). My host has the mysqlnd driver, but it doesn't have the nd_mysqli driver. That could be relevant. I'm not sure.

I think I found a way to replace

// assumes execution of a prepared statement
// for example: "SELECT * FROM table WHERE important_field LIKE ?"

$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result,MYSQLI_NUM);

I did something weird with arrays and references.

$row = array();
$references_array2 = array();
for($i=0; $i<$stmt->field_count; $i++){
    $row[] = "";
    $references_array2[] = &$row[$i];
}
array_unshift($references_array2,$stmt);
$status = call_user_func_array("mysqli_stmt_bind_result",$references_array2);

Whenever I want to populate the row array, I call:

$fetch_status = mysqli_stmt_fetch($stmt);

I check the fetch_status variable in a while loop or an if statement according to the rules in the documentation: https://www.php.net/manual/en/mysqli-stmt.fetch.php

I recommend using the triple equals operator ("===") when comparing against NULL or FALSE to avoid any confusion between the two values (and types).

if ($fetch_status === NULL){
    // No more rows/data exists or data truncation occurred

} elseif ($fetch_status === FALSE) {
    // Error occurred

} // more code, etc.