What does the PHP function mysql_fetch_assoc do?
The PHP function mysql_fetch_assoc is used to fetch a single row from a result set returned by a MySQL query and returns it as an associative array. This function is commonly used in conjunction with other MySQL functions to retrieve data from a database and process it in PHP. To use mysql_fetch_assoc, you need to first establish a connection to a MySQL database using functions like mysql_connect and mysql_select_db. Then, you can execute a query using mysql_query and use mysql_fetch_assoc to fetch rows from the result set one by one until all rows have been processed.
// Establish a connection to the MySQL database
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $connection);
// Execute a query
$result = mysql_query("SELECT * FROM table_name");
// Fetch rows from the result set using mysql_fetch_assoc
while ($row = mysql_fetch_assoc($result)) {
// Process the data in $row
echo $row['column_name'];
}
// Close the connection
mysql_close($connection);