Current Location: Home> Latest Articles> Understanding the Meaning and Usage of 'res' in PHP

Understanding the Meaning and Usage of 'res' in PHP

M66 2025-08-04

What is 'res' in PHP

In PHP development, res is typically used as a variable to store the result set from a database query. It represents a resource type and is commonly used with functions like mysqli_query() to manage MySQL query results.

Using res to Fetch Query Results

You can execute an SQL query using mysqli_query() and assign the returned result to the res variable. Here’s an example:

$res = mysqli_query($conn, "SELECT * FROM users");

Explanation:

  • $conn refers to the database connection object
  • "SELECT * FROM users" is the SQL query to be executed

If the query is successful, $res holds the result set resource that can be processed further.

Common Operations on res

To retrieve and iterate over the result set, you can use the following functions:

  • mysqli_fetch_array(): Returns a row as both an associative and numeric array
  • mysqli_fetch_assoc(): Returns a row as an associative array
  • mysqli_fetch_row(): Returns a row as a numeric array
  • mysqli_fetch_object(): Returns a row as an object

Select the appropriate function depending on your specific use case to handle the data stored in res.

Freeing the res Resource

After processing the query results, it's good practice to free the memory associated with res using mysqli_free_result():

mysqli_free_result($res);

Freeing resources is an essential habit, especially when working with large datasets, as it helps improve performance and reduce load on the database connection.

Conclusion

In PHP, res is a key variable used for handling database query results. Understanding how to use it effectively and clean up afterwards can lead to more efficient and maintainable code when working with MySQL.