What function should be used to fetch the result of a MySQL query in PHP?
To fetch the result of a MySQL query in PHP, you should use the `mysqli_fetch_assoc()` function. This function fetches a result row as an associative array. You can then access the data from the query result using the keys of the associative array.
// Assuming $conn is your MySQL database connection and $query is your SQL query
$result = mysqli_query($conn, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
// Access data from the query result using $row['column_name']
echo $row['column_name'];
}
} else {
echo "Error: " . mysqli_error($conn);
}
Related Questions
- What are common issues that can arise when running a PHP script on different environments, such as a local laptop setup versus an online server?
- What is the potential issue with using the same query multiple times in PHP when populating a dropdown menu from a database?
- What considerations should be taken into account when using complex conditional statements in PHP code, especially for beginners or future reference?