Home  >  Article  >  Database  >  Why Does My PHP Code Throw a \'Fatal Error: Call to Undefined Function mysqli_result()\'?

Why Does My PHP Code Throw a \'Fatal Error: Call to Undefined Function mysqli_result()\'?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-24 02:12:09922browse

Why Does My PHP Code Throw a

Understanding the "Fatal Error: Call to Undefined Function mysqli_result()" Exception in PHP

When attempting to switch from the deprecated mysql functions to sqli in PHP, you may encounter the error "Fatal error: Call to undefined function mysqli_result()". This error occurs when you try to access the result of a MySQL query using the mysqli_result() function, which is not available in the updated mysqli extension.

Solution: Using mysqli_fetch_assoc()

To resolve this issue, avoid directly accessing the result using mysqli_result() and instead use the mysqli_fetch_assoc() function to retrieve the data in an associative array. This function iterates through the result set and returns the next row as an associative array, making it more efficient and compatible with the mysqli extension. Here's how you can rewrite your code using mysqli_fetch_assoc():

$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'];
}

Benefits of mysqli_fetch_assoc()

Using mysqli_fetch_assoc() offers several advantages:

  • Increased efficiency: It performs a single database operation instead of multiple operations.
  • Enhanced portability: The code is compatible with the updated mysqli extension.
  • Improved security: It helps prevent SQL injection vulnerabilities.

The above is the detailed content of Why Does My PHP Code Throw a \'Fatal Error: Call to Undefined Function mysqli_result()\'?. 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