-1

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

Red
  • 26,798
  • 7
  • 36
  • 58
Magform
  • 3
  • 1
  • 3
  • 1
    Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) You should consider using [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenated values – RiggsFolly Jul 16 '20 at 13:08
  • It's my first experience with databases and things like that, thanks for the advice we immediately throw an eye, this should also solve the problem of connection with the database or should it be solved in another way? – Magform Jul 16 '20 at 13:14
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jul 16 '20 at 13:20
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jul 16 '20 at 13:21
  • You need to specify the port. Your database is listening on port 3308. If you are going to use mysqli then please find a better tutorial, however strongly recommend learning PDO instead which is much easier. – Dharman Jul 16 '20 at 13:25
  • I immediately look at the posts recommended by you, for the connection problem with the database, how can I solve it instead? (it's my first experience with mySQL for now it's all for testing so security I thought to implement it later) – Magform Jul 16 '20 at 13:27

1 Answers1

0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks a lot, I had not noticed this now, however, the problem seems to have increased, still unable to read the database, this is the error https://imgur.com/a/qIeKnx0 – Magform Jul 16 '20 at 13:19
  • This was a typo in the question. The actual problem seems to be confusion between two database servers listening one 3306 and 3308 – Dharman Jul 16 '20 at 13:26
  • @Dharman Yes, just saw that and amended the answer accordingly – RiggsFolly Jul 16 '20 at 13:28
  • Cool, but I remember there was a much better duplicate explaining this, possible even written by you. I don't have time to search for it now, but you could link it instead. – Dharman Jul 16 '20 at 13:29
  • Thanks a lot you solved my problem now I dedicate myself to implementing some security as recommended by Dharman – Magform Jul 16 '20 at 13:31