PHP mysqli_fetch_assoc() Function

Learn via video courses
Topics Covered

Overview

The mysqli_fetch_assoc() function in PHP's MySQL Improved Extension (mysqli) retrieves rows from a query result as an associative array, using column names as keys. This contrasts with numeric indices. This article explores its syntax, parameters, return values, and usage, offering examples for clarity.

Syntax of PHP mysqli_fetch_assoc() Function

The syntax of the mysqli_fetch_assoc() function is relatively simple:

Parameter Values of PHP mysqli_fetch_assoc() Function

The mysqli_fetch_assoc() function takes a single parameter, which is the result set obtained from a SELECT query. This parameter is mandatory and serves as the reference to the result set that the function fetches data from.

Return Values of PHP mysqli_fetch_assoc() Function

The primary return value of the mysqli_fetch_assoc() function is an associative array representing a row of data from the result set. The array's keys correspond to the column names, while the values contain the data retrieved from those columns. The function returns NULL when there are no more rows to be fetched from the result set.

Object-Oriented Style

In the object-oriented style of using the mysqli extension, the mysqli_fetch_assoc() function is called on a mysqli_result object. Here's an example:

Procedural Style

In the procedural style, the mysqli_fetch_assoc() function is called with the result set obtained through the mysqli_query() function. Here's an example:

Comparison of mysqli_result Iterator and mysqli_result::fetch_assoc() Usage

The mysqli extension also provides another way to iterate through result sets using the mysqli_result iterator. However, the mysqli_fetch_assoc() function offers a more concise and intuitive approach. When comparing the two methods:

  • The mysqli_result iterator requires less code but lacks the clarity of associating column names directly with data. It uses numeric indices for column access.
  • The mysqli_fetch_assoc() provides a more readable and semantic way of accessing data by associating column names with their respective values.

Thus, while the mysqli_result iterator might be suitable for simple scenarios, the mysqli_fetch_assoc() function shines when clarity and maintainability are essential.

More Examples

To understand the concepts better, let's consider a few examples of using the mysqli_fetch_assoc() function:

Example - 1: Fetching User Data

Explanation:

In this example, an SQL query is constructed to retrieve specific columns (id, username, and email) from a "users" table using the mysqli_query() function. The mysqli_fetch_assoc() function is then used in a loop to fetch each row of data as an associative array. Inside the loop, the individual elements of the array are accessed using keys (e.g., $row['id'], $row['username']) and printed to display user information such as user ID, username, and email.

Example - 2: Generating an HTML Table

Explanation:

In this example, a SQL query is used to select product information (product_name, price, and stock_quantity) from a "products" table using mysqli_query(). Subsequently, a dynamic HTML table is generated using a while loop combined with mysqli_fetch_assoc(). For each row fetched, a new table row (<tr>) is created, and the product details are inserted into the appropriate table cells (<td> elements). This process generates an HTML table displaying product data in a structured format, with columns for product name, price, and stock quantity. The echoed HTML code results in the formation of the complete table structure.

Conclusion

  • The mysqli_fetch_assoc() function is used to fetch rows from a result set as an associative array in PHP.
  • Its syntax involves passing the result set as a parameter.
  • The function returns an associative array with column names as keys.
  • It offers both object-oriented and procedural styles for usage.
  • When compared to the mysqli_result iterator, mysqli_fetch_assoc() provides a more readable and organized way of accessing data.
  • Examples demonstrate its usefulness in various scenarios, from displaying user data to generating HTML tables.
  • By mastering the mysqli_fetch_assoc() function, developers can enhance their ability to efficiently retrieve and manipulate data from database result sets, contributing to more effective and streamlined web applications.