How can the "No database selected" error be resolved when using PHP and MySQL?
The "No database selected" error occurs when a database is not specified in the connection to MySQL in PHP. To resolve this issue, you need to explicitly select the database after establishing the connection using the mysqli_select_db() function.
// Establish connection
$conn = mysqli_connect("localhost", "username", "password");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Select the database
$db_selected = mysqli_select_db($conn, "database_name");
if (!$db_selected) {
die ("Database selection failed: " . mysqli_error($conn));
}
Related questions
-
PHP cron jobs email sending
-
sorting arrays usort
-
cURL PHP variables