Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

mysqli_stmt::get_result() is undefined on a prepared statement

ErrorFatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result()

Short answer

get_result() exists only when PHP's mysqli is built on the MySQL Native Driver (mysqlnd). If your PHP was built on the older libmysqlclient, the method is missing. Check which library you run. On PHP 8.2 and newer mysqlnd is the only option; on an older build, upgrade PHP or read rows with bind_result() instead.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the loaded PHP extensions for the PHP that runs your site. If mysqlnd is not in the list, mysqli is using a different client library and get_result() is not available.

    php -v
    php -m | grep -i -E "mysqli|mysqlnd"
  2. 2Match what you see, then jump to its fix

    1. 1mysqli is listed but mysqlnd is not; PHP is older than 8.2mysqli was compiled against libmysqlclient, not mysqlndFix
    2. 2php -v shows 5.3 or lowerPHP is 5.3 or older, from before mysqlnd was the defaultFix
    3. 3Shared hosting or a locked PHP build where mysqlnd is not availableYou cannot change the server, so fetch rows another wayFix

What does the error look like?

  • Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result()
  • Call to undefined method mysqli_stmt::get_result()

Cause 1 of 3

mysqli was compiled against libmysqlclient, not mysqlnd

1Check

The manual's build matrix says mysqlnd is the default from PHP 5.4 and the only option from PHP 8.2. A build from PHP 5.4 to 8.1 made with --with-mysqli=/path/to/mysql_config uses libmysqlclient instead, and get_result() is documented as available only with mysqlnd.

php -m | grep -i -E 'mysqli|mysqlnd'

2Fix

Move to PHP 8.2 or newer, where mysqlnd is the only client library. If you compile PHP yourself, configure it with plain --with-mysqli and leave out the /path/to/mysql_config form.

./configure --with-mysqli [other options]

3Verify

Run the script again, or ask PHP whether the method now exists.

php -r 'var_dump(method_exists("mysqli_stmt", "get_result"));'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

PHP is 5.3 or older, from before mysqlnd was the default

1Check

In the manual's matrix, PHP 5.3 defaults to libmysqlclient and PHP 5.0 to 5.2 cannot use mysqlnd at all.

php -v

2Fix

Upgrade PHP. Old releases no longer get security fixes, so a version bump fixes this error and the missing patches together.

# install a supported PHP release and its MySQL extension (mysqli)
# with your distribution's package manager, then restart PHP

3Verify

Confirm the new version and the extension.

php -v
php -m | grep -i mysqli

Confirmed by php.net

Did this fix it?

Cause 3 of 3

You cannot change the server, so fetch rows another way

1Check

The manual lists bind_result() with fetch() as the way to read a prepared statement without get_result(). It needs one variable per selected column, bound after execute().

grep -rn 'get_result' --include=*.php .

2Fix

Replace get_result() and fetch_assoc() with bind_result() and a fetch() loop. Call store_result() before fetching if you want a buffered result set.

$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($outId, $outLabel);
while ($stmt->fetch()) {
    echo $outId, " ", $outLabel, "\n";
}

3Verify

Run the page and check that rows print without the fatal error.

php your-script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run PHP 8.2 or newer in every environment, so local, staging and production use the same client library.
  • Use the same PHP version for the command line and the web server, and check php -m on both.
  • Keep mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) at the top of database code so failures throw instead of returning false.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.