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

I’m having trouble connecting to my database after reinstalling WAMP server. I’m using PHP PDO, but I keep getting an access denied error. Here’s my connection code:

class DbConnect {
    private $host = 'localhost';
    private $db = 'shop_system';
    private $user = 'root';
    private $pass = '';
    public $connection;

    public 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, but it’s still not working. Could it be a permissions issue with WAMP?

Hey mate, have u tried checkin ur phpMyAdmin? Sometimes after reinstallin WAMP, the default user/pass combo gets messed up. Try loggin in there first to make sure ur creds are correct. Also, double-check if ur using the right port number in ur connection string. If all else fails, u might need to tweak ur MySQL config file. good luck!

Have you checked if the MySQL service is running properly after reinstalling WAMP? Sometimes, the service doesn’t start automatically. Try stopping and restarting the MySQL service from the WAMP tray icon. Also, verify that your MySQL port (usually 3306) isn’t being used by another application. If these don’t work, try creating a new user with all privileges and use those credentials instead of root. Lastly, ensure your php.ini file has the MySQL extension enabled. These steps often resolve connection issues after a fresh WAMP installation.