Unable to connect to MySQL Database...Why?

Hey guys, I’m new to PHP and I’m creating a login and register using PHP but then how do I actually connect them together. Cause I keep on getting connection failed on the webpages when direct it. And I’m using xampp to do this too.

The database name is database and the table name is users

Below are my code:-

Server Code

<?php
  session_start();

  //Create Connection
  $connection = mysqli_connect('localhost', 'root', '');

  mysqli_select_db($connection, 'database');

  //Check Connection
  if(!$connection -> connect_error){
    die("Connection Failed!" . mysqli_connect_error());
  }

  $email = $_POST['email'];
  $fullname = $_POST['fullname'];
  $password = $_password['password'];
  $birthday = $_POST['birthday'];

?>

Sign Up Code

<?php
  include('server.php');

  $sqlCheck = "select * from users where email = '$email' ";
  $result = mysqli_query($connection, $sqlCheck);
  $num = mysqli_num_rows($result);

  //Check if the email have registered
  if($num == 1){
    echo "Email have been registered!";
  } else {
    $sqlInsert = "insert into users(email,fullname,password,birthday)
                VALUES ('$email','$fullname','$password','$birthday')";

    mysqli_query($connection, $sqlInsert);
    echo "Successfully Registered!";
  }
?>

Login Code

<?php
  include('server.php');

  $sqlCheck = "select * from usertable where email = '$email' && password='$password'";
  $result = mysqli_query($connection, $sqlCheck);
  $num = mysqli_num_rows($result);

  //Check if the email have registered
  if($num == 1){
    header('location: user_home.php');
  } else {
    header('location: login.php');
  }
?>

Error

You are mixing procedural statements with OOP (Object Oriented Programming) statements. While this generally works with the mysqli extension, it doesn’t work when making a connection using mysqli_connect(), because you get a false value (this is what the original procedural code at that point was testing) if there is a connection error, rather than a mysqli object. You should stick with one programming style throughout your code. You also have a logic mistake in the use of the ! (not). A connection error would be a true value. Adding the ! (not) is testing for not a connection error, so, your logic is outputting the connection failed message whenever there actually is a connection. This is the reason why there is no output from the mysqli_connect_error() call.

If you are just starting out, forget about the mysqli extension, It is overly complicated and inconsistent, hence the problems testing for a connection error. Instead, learn the PDO extension. It is much simpler and more consistent, especially when using prepared queries, which you should be using whenever supplying external, unknown, dynamic values to a query.

Also, if you use exceptions for database statement errors, for either the msyqli or PDO extension, and in most cases let php catch and handle the exception, php will use its error related settings to control what happens with the actual error information (database statement errors will automatically get displayed/logged the same as php errors.) This will let you remove any existing database statement error handling logic, simplifying your code, which will also eliminate the current logic mistake in the connection error handling.

2 Likes

Ah, so it is my logic then. Sorry about that. I have a problem with my logic now when I see it because it, not the first time someone says about my logic problem haha but do you know how I can improve my logic in coding and ways or any website or any practice site that is good to practice logic. I want to enhance my weakness, now that I know. Really sorry

Early on in your coding journey, it’s pretty much like writing practice; the more you do the better you get. For PHP specifically, PHP The Right Way is worth a read for best practices on many parts of the language. For practising logic, search online for coding puzzles to play with.

Sponsor our Newsletter | Privacy Policy | Terms of Service