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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.