Home  >  Article  >  Database  >  Why is `mysqli_result()` undefined and how can I efficiently retrieve data from a MySQLi result set?

Why is `mysqli_result()` undefined and how can I efficiently retrieve data from a MySQLi result set?

Barbara Streisand
Barbara StreisandOriginal
2024-11-19 17:29:02739browse

Why is `mysqli_result()` undefined and how can I efficiently retrieve data from a MySQLi result set?

Undetermined Function Invocation: "Fatal error: Call to undefined function mysqli_result()""

The provided code snippet attempts to migrate from MySQL to mysqli but encounters an error message indicating that the mysqli_result function is undefined. Let's delve into the cause and explore an alternative solution.

The original code utilized the mysql_result function, which is part of the deprecated MySQL extension. When switching to the MySQL Improved (mysqli) extension, you must replace the mysql functions with their mysqli counterparts. However, there is no direct equivalent for mysql_result in mysqli.

The solution lies in leveraging mysqli_fetch_assoc, a more efficient approach for retrieving data from a result set. The following modified code demonstrates this approach:

$query = ("SELECT * FROM `product_category`");
$result = mysqli_query($connect, $query) or die("could not perform query");
$num_rows = mysqli_num_rows($result);

while ($row = mysqli_fetch_assoc($result)) {
    $ID = $row['ID'];
    $name = $row['name'];
    $description = $row['description'];
}

This single database operation eliminates the need for the multiple operations performed using mysql_result, resulting in improved efficiency.

The above is the detailed content of Why is `mysqli_result()` undefined and how can I efficiently retrieve data from a MySQLi result set?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn