skip to Main Content

I’m trying to create an admin login using PHPMyAdmin, and I can’t seem to query to the database. I wanted to know if this would be an issue with my code, or if this is an issue with how I have set up my database (missing something to connect to database).

So I’ve been catching mistakes (such as PHP changing mysql to mysqli), but I cannot connect to the database properly. Here are some backend approaches I’ve done to set up my database:

  1. Database is downloaded
  2. PHPMyAdmin is accessible through localhost
  3. I’ve created a database on PHPMyAdmin called "pcc_website_login" (with a username and password to login)
  4. MySQL is currently active
  5. The errors are from mysqli_error/query/connect, and all state that I cannot connect to the database correctly.

Below is the code for process.php; I also have a login.php/style.css, but those don’t really pertain to the database.



<?php
    // get values passed from form in login.php file
    $username = $_POST['user'];
    $password = $_POST['pass'];

    $username = stripcslashes($username);
    $password = stripcslashes($password);

    $db = mysqli_connect("localhost","root","","pcc_website_login");

    $result = mysqli_query($db, "select * from login where username = '$username' and password = '$password'") or die("Failed to query database " .mysqli_error());

    $row = mysqli_fetch_array($result);
    
    if ($row['username']== $username && $row['password'] == $password){
        echo "Login success!!! Welcome " .$row['username'];
    } else {
        echo "Faild to login!";
    }

?>

3

Answers


  1. You can test connect to your database. See example.

    <?php
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    
    echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
    
    mysqli_close($link);
    ?>
    
    Login or Signup to reply.
  2. Try with this to figure out your problem

    $db = mysqli_connect("localhost","root","","pcc_website_login");
    // Check connection
    if ($db-> connect_errno) {
      echo "Failed to connect to MySQL: " . $db-> connect_error;
      exit();
    }
    
    Login or Signup to reply.
  3. Hey looks like your just starting out with PHP and MySQL… its a crazy adventure, good job trying on something new. There few nail biting things wrong, but their mistakes I’ve made myself and its a good way to learn so don’t let that stop you.

    First for your connection problem, unless you removed it, you’re missing a password.

    $db = mysqli_connect("localhost","root", "password" ,"pcc_website_login");

    The same user name and password that you use with phpMyAdmin will work… because after all phpMyAdmin is ran by the same kind of php you are using.

    Second

    Don’t use stripcslashes, it will not protect you. use mysqli_real_escape_string instead w3chools.com has a good example

       $username = stripcslashes($username);  //NO, BAD
       $password = stripcslashes($password);
    
       $username = $db->mysqli_real_escape_string($username); // Better
       $password = $db->mysqli_real_escape_string($password);
    

    Third

    Absolutely NEVER store raw passwords in a database for any reason… ever.

    You could hash of the password with the user name and another secret through a cryptographic function and, then store that.

    Better still is to use OAUTH2 "Sign in with google" or "Sign in with Facebook". To use that you will need a valid domain and proper SSL certificate, running it from your local computer won’t work either unless and its not worth the hassle as cheap as hosting is

    Finally

    Don’t be afraid to use somebody else’s PHP code in your project. Compare being a program developer to say a housing developer. In that case you’d start with similar floor plans that the client was looking for and just tweak them to fit. Usig a Framwork like Laravel or Symfony, or a CMS like Joomla or WordPress is far easier.

    In any case have fun and keep playing, have fun with the discovery, make things that solve silly but useful problems for you or friends, and challenge yourself to write as little code as possible to make that happen.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search