$query = "SELECT * FROM users"; $result = mysqli_query($connection, $query); while($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row["id"] . " Name: " . $row["name"] . " Email: " . $row["email"]; }
$query = "SELECT * FROM products"; $result = $db->query($query); while($row = $result->fetchAssoc()) { echo "Product Name: " . $row["name"] . " Description: " . $row["description"]; }In these examples, the `fetchAssoc` method is used to retrieve data from a MySQL database. The first example uses the MySQLi library while the second example uses a database package library such as Doctrine or Laravel's Eloquent. Overall, the `fetchAssoc` method is an essential tool for accessing data from a database table. It allows developers to retrieve data efficiently and seamlessly convert it into an associative array for better handling.