How can the mysqli_fetch_assoc function be used in PHP code?

The mysqli_fetch_assoc function in PHP is used to fetch a single row from a result set returned by a query and return it as an associative array. To use this function, you need to first establish a connection to a MySQL database using the mysqli_connect function, execute a query using mysqli_query, and then fetch the result using mysqli_fetch_assoc.

// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Execute a query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Fetch the result as an associative array
$row = mysqli_fetch_assoc($result);

// Access the data using keys
echo $row['column_name'];