Database connection error: Access denied for localhost user to 'ecom' database

I'm having trouble connecting to my database after reinstalling WAMP server. When using PHP PDO, I get an access denied error. Here's my connection code:

class DBConnect {
    private $host = 'localhost';
    private $db = 'myshop';
    private $user = 'root';
    private $pass = '';
    public $connection;

    function __construct() {
        if (!isset($this->connection)) {
            try {
                $dsn = "mysql:host={$this->host};dbname={$this->db}";
                $this->connection = new PDO($dsn, $this->user, $this->pass);
                $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                echo "Connection failed: " . $e->getMessage();
                exit;
            }
        }
    }
}

Any ideas why I'm getting this error? I've double-checked my database name and user credentials.

Hey there! Have you tried connecting to the database using a GUI tool like HeidiSQL or MySQL Workbench? Sometimes it’s easier to troubleshoot connection issues that way. You could also check your MySQL error logs for any clues - they’re usually in the WAMP installation directory under logs/mysql.log. Oh, and don’t forget to make sure you’ve got the right PHP extensions enabled in your php.ini file. PDO and mysqli should both be uncommented. Just thinking out loud here, but have you considered using a .env file for your database credentials? It’s a bit more secure and makes it easier to switch between environments. Anyway, let us know if you make any progress! This stuff can be frustrating, but we’ve all been there.

hey man, i had the same problem. try checking ur phpmyadmin. sometimes the default user isnt root anymore. Also, make sure u got the right port number. WAMP usually uses 3306 but it could be different. if none of that works, try restarting ur computer. that fixed it for me once lol

I’ve encountered similar issues after reinstalling WAMP. Have you verified that MySQL is actually running? Sometimes it doesn’t start automatically after a fresh install. Check your WAMP icon in the system tray - it should be green. If it’s not, try starting MySQL manually from the WAMP menu. Also, ensure you’ve created the ‘myshop’ database. If it doesn’t exist, MySQL will deny access. Lastly, double-check your MySQL root password. If you’ve set one during installation, you’ll need to update the ‘pass’ field in your connection code. Let me know if any of these steps resolve your issue.