In the context of PHP and MySQL, how can PHP variables be populated with values retrieved from a database query?
To populate PHP variables with values retrieved from a database query in PHP and MySQL, you can use the mysqli_fetch_assoc() function to fetch a row from a result set as an associative array. You can then assign the values from the associative array to PHP variables.
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Run a query to retrieve data
$query = "SELECT column1, column2 FROM table";
$result = mysqli_query($connection, $query);
// Fetch a row as an associative array
$row = mysqli_fetch_assoc($result);
// Assign values to PHP variables
$variable1 = $row['column1'];
$variable2 = $row['column2'];
// Close the connection
mysqli_close($connection);