Skip to content

A Comprehensive Guide on mysqli_use_result Function in PHP

When working with MySQL databases in PHP, the mysqli extension provides various functions for database operations. One such function is mysqli_use_result, which initiates an unbuffered result set retrieval. This approach is particularly useful for handling large datasets efficiently.

In this guide, we will provide you with a comprehensive understanding of the mysqli_use_result function, its features, and how to use it effectively in your PHP projects.

What is mysqli_use_result Function?

The mysqli_use_result function is a built-in PHP function that initiates an unbuffered result set retrieval and returns a result object for further processing. Unlike the default buffered query mode, which loads the entire result set into memory, mysqli_use_result fetches rows one by one. This makes it ideal for retrieving large result sets in a more memory-efficient manner.

The mysqli_use_result function takes one argument, which is the MySQL connection resource returned by the mysqli_connect function.

Here is the syntax of the mysqli_use_result function:

php
mysqli_use_result($connection);

Features of mysqli_use_result Function

The mysqli_use_result function provides a variety of features that make it a useful tool for retrieving large result sets from MySQL databases in a more memory-efficient approach. Some of the key features of the function include:

1. Initiating Result Set Retrieval

The main feature of the mysqli_use_result function is to initiate an unbuffered result set retrieval from MySQL databases. This allows you to retrieve large result sets in a more memory-efficient manner by fetching rows sequentially rather than loading them all at once.

2. Memory Efficiency

By avoiding the buffering of the entire result set, mysqli_use_result significantly reduces memory consumption. This is especially beneficial when working with large tables or complex queries that return thousands of rows.

How to Use mysqli_use_result Function

Here are some steps to use the mysqli_use_result function in your PHP projects:

1. Connecting to MySQL Server

Before you can use the mysqli_use_result function, you need to establish a connection to the MySQL server using the mysqli_connect function. Here is an example code snippet:

php
<?php

$host = 'localhost';
$user = 'username';
$password = 'password';
$database = 'mydatabase';

$connection = mysqli_connect($host, $user, $password, $database);

if (!$connection) {
    die('Connection failed: ' . mysqli_connect_error());
}

2. Initiating Result Set Retrieval

Once you have established a connection to the MySQL server, you must first execute a SQL query using mysqli_query(). Then, call mysqli_use_result() to initiate unbuffered retrieval. After fetching the rows, free the result memory. Here is an example code snippet:

php
$sql = "SELECT * FROM users";
mysqli_query($connection, $sql);

$result = mysqli_use_result($connection);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row
        print_r($row);
    }
    mysqli_free_result($result);
}

Note: Unbuffered result sets do not support mysqli_num_rows() or mysqli_data_seek().

This code initiates a result set retrieval using the mysqli_use_result function, iterates through the unbuffered rows, and properly frees the memory afterward.

Conclusion

In conclusion, the mysqli_use_result function is a useful tool for retrieving large result sets from MySQL databases in a more memory-efficient manner in PHP. It provides key features such as unbuffered result set retrieval and reduced memory consumption. By following the steps outlined in this guide, you can use the mysqli_use_result function effectively in your PHP projects to handle large datasets without exhausting server memory.

Practice

What are the ways to use a result from MySQL in PHP?

Dual-run preview — compare with live Symfony routes.