Database Selection Failed Unknown database 'login'

I was trying to create a login using PHP and MySQL for the database, I did everything but when I try to run the site it gives me a connection error with the database even though I created the database and gave it the corresponding name (login)

This is the code

db_connect.php

 <?php $connection = mysqli_connect('localhost', 'root', ''); if (!$connection){     die("Database Connection Failed" . mysqli_error($connection)); } $select_db = mysqli_select_db($connection, 'login'); if (!$select_db){     die("Database Selection Failed" . mysqli_error($connection)); } 

authen_login.php

 <?php    require('db_connect.php');  if (isset($_POST['user_id']) and isset($_POST['user_pass'])){      // Assigning POST values to variables. $username = $_POST['user_id']; $password = $_POST['user_pass'];  // CHECK FOR THE RECORD FROM TABLE $query = "SELECT * FROM `user_login` WHERE username='$username' and Password='$password'";   $result = mysqli_query($connection, $query) or die(mysqli_error($connection)); $count = mysqli_num_rows($result);  if ($count == 1){  //echo "Login Credentials verified"; echo "<script type='text/javascript'>alert('Login Credentials verified')</script>";  }else{ echo "<script type='text/javascript'>alert('Invalid Login Credentials')</script>"; //echo "Invalid Login Credentials"; } } ?>  

And the error is this

Database Selection Failed Unknown database 'login'

(I add to the question a photo of the database that maybe the error is there)

enter image description here

Add Comment
1 Answer(s)

You dont have a database called shoolbrk, your database appears to be called login.

Also you you should use mysqli_connect_error() to see connection errors and not mysqli_error($connection). Thats for all other errors other than connection errors.

You can also simplify the connection script as follows

<?php $connection = mysqli_connect('localhost', 'root', '', 'login'); //                                       database name ^^^^^ if (!$connection){     echo $connection ->connect_error; } 

A better solution would also be to not die() or show errors to the world, instead add mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the connection script.

Port number issue

See this answer https://stackoverflow.com/a/62831626/2310830 which describes how WAMPServer manages MySQL and mariaDB port number usage. And how it will allow you to switch the default to MySQL so it uses port 3306 which will allow you to write your code in a way that will run anywhere without having to change the port number. As most hosting companies will have either MySQL or mariaDB listening on 3306

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.