How can the error "No database selected" in PHP be fixed when using MySQL queries?
When the error "No database selected" occurs in PHP while using MySQL queries, it means that a specific database has not been selected for the query to run on. To fix this issue, you need to explicitly select the database using the `mysqli_select_db()` function before executing any queries.
// Connect to MySQL server
$connection = mysqli_connect("localhost", "username", "password");
// Select the database
mysqli_select_db($connection, "database_name");
// Perform MySQL query
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);
// Process the query result
if ($result) {
// Process the result
} else {
echo "Error: " . mysqli_error($connection);
}
// Close the connection
mysqli_close($connection);