-2

I am trying to make a user registration system using PDO in PHP, and I'm unable to connect to the MySQL database. It always says access denied and there is some fatal error as well.

I tried resetting MySQL and other things available on the Internet, but nothing worked.

<?php
    class userClass
    {
         /* User Login */
         public function userLogin($usernameEmail, $password)
         {
              $db = getDB();
              $hash_password = hash('sha256', $password);
              $stmt = $db->prepare("SELECT uid FROM users WHERE  (username=:usernameEmail or email=:usernameEmail) AND  password=:hash_password");
              $stmt->bindParam("usernameEmail", $usernameEmail, PDO::PARAM_STR);
              $stmt->bindParam("hash_password", $hash_password, PDO::PARAM_STR);
              $stmt->execute();
              $count = $stmt->rowCount();
              $data = $stmt->fetch(PDO::FETCH_OBJ);
              $db = null;
              if ($count)
              {
                  $_SESSION['uid'] = $data->uid;
                  return true;
              }
              else
              {
                  return false;
              }
         }

         /* User Registration */
         public function userRegistration($username, $password, $email, $name)
         {
              try{
                  $db = getDB();
                  $st = $db->prepare("SELECT uid FROM users WHERE username=:username OR email=:email");
                  $st->bindParam("username", $username,PDO::PARAM_STR);
                  $st->bindParam("email", $email,PDO::PARAM_STR);
                  $st->execute();
                  $count = $st->rowCount();
                  if ($count<1)
                  {
                      $stmt = $db->prepare("INSERT INTO users(username,password,email,name) VALUES (:username,:hash_password,:email,:name)");
                      $stmt->bindParam("username", $username,PDO::PARAM_STR);
                      $hash_password= hash('sha256', $password);
                      $stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR);
                      $stmt->bindParam("email", $email,PDO::PARAM_STR);
                      $stmt->bindParam("name", $name,PDO::PARAM_STR);
                      $stmt->execute();
                      $uid = $db->lastInsertId();
                      $db = null;
                      $_SESSION['uid'] = $uid;
                      return true;
                  }
                  else
                  {
                      $db = null;
                      return false;
                  }
              }
              catch(PDOException $e) {
                  echo '{"error":{"text":' . $e->getMessage() . '}}';
              }
         }

         /* User Details */
         public function userDetails($uid)
         {
             try{
                 $db = getDB();
                 $stmt = $db->prepare("SELECT email,username,name FROM users WHERE uid=:uid");
                 $stmt->bindParam("uid", $uid,PDO::PARAM_INT);
                 $stmt->execute();
                 $data = $stmt->fetch(PDO::FETCH_OBJ);
                 return $data;
             }
             catch(PDOException $e) {
                 echo '{"error":{"text":' . $e->getMessage() . '}}';
             }
         }
    }
?>

These are the errors I'm getting.

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

and

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\auctionsystem\class\userClass.php:33 Stack trace: #0 C:\xampp\htdocs\auctionsystem\index.php(40): userClass->userRegistration('anurag', 'anurag123', 'anurag@email.co...', 'Anurag Pal') #1 {main} thrown in C:\xampp\htdocs\auctionsystem\class\userClass.php on line 33

yivi
  • 42,438
  • 18
  • 116
  • 138
anurag
  • 11
  • 4
  • 1
    Looks like `getDB()` returned null. You haven't shown the `getDB()` function so there's no way to guess what you're doing wrong in that function. Based on the error, I'd guess that in that function, you aren't sending any password when connecting to MySQL using `new PDO(...)` – Bill Karwin Nov 09 '19 at 18:09
  • 1
    See code examples here: https://www.php.net/manual/en/pdo.construct.php for correct use of `new PDO(...)`. – Bill Karwin Nov 09 '19 at 18:10
  • Couldn't [similar](https://duckduckgo.com/html/?q=site%3Astackoverflow.com+Connection%20failed%3A%20SQLSTATE%5BHY000%5D%20%5B1045%5D%20Access%20denied%20for%20user%20%27root%27%40%27localhost%27%20(using%20password%3A%20NO)) [questions](https://stackoverflow.com/search?q=%5BPHP%5D+Connection+failed+SQLSTATE+HY000+1045+Access+denied+for+user+using+password+NO) provide a clue? – Peter Mortensen Nov 09 '19 at 18:21
  • [Sample](https://stackoverflow.com/questions/13841748/giving-permissions-to-user-doesnt-work-in-mysql). – Peter Mortensen Nov 09 '19 at 18:54
  • Does this answer your question? [ERROR 1698 (28000): Access denied for user 'root'@'localhost'](https://stackoverflow.com/questions/39281594/error-1698-28000-access-denied-for-user-rootlocalhost) – Dharman Nov 09 '19 at 22:55

1 Answers1

-1

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

This means that you attempted to connect to a database and it denied the connection for whatever reason

I do not suggest using root, create a user besides root to use!

  • Try to make sure your user account is not set to only allow local connections only
  • Set your user account password to match what you provided
  • Check to see if your firewall is restricting your connection
  • Ensure your SQL User allows any host to connect (identified by %)