What role does the mysql_fetch_assoc function play in the context of the PHP script, and how does it impact the retrieval of data from the database?
The mysql_fetch_assoc function in PHP is used to retrieve a single row from a result set as an associative array. This function is commonly used in conjunction with querying a MySQL database to fetch data. It allows for easier access to the retrieved data by using column names as keys in the associative array.
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query the database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Fetch data using mysql_fetch_assoc
while ($row = mysqli_fetch_assoc($result)) {
// Access data using column names as keys
echo $row['column_name'];
}
// Close the connection
mysqli_close($connection);