Cannot connect to MySQL Database using the WAMP server

10.6k views Asked by At

I am new to programming and I'm totally stuck, so I thought I will ask experienced programmers for help. I created a SQL database called "books" using phpMyadmin on WAMP server. The database and its tables show properly on phpMyadmin and also on the MySQL console. I've been trying to connect to the database using the following PHP code:

    $hs = "localhost";
    $us = "heman";
    $ps = "password";
    $dbs = "books";

    $db = new mysqli($hs, $us, $ps, $dbs);

    if (mysqli_connect_error()) {
        echo "Error! Could not connect to the database." . $db->connect_error . $db->connect_errno;
    exit;
    }   else {echo "Connected to Server.";};

And, this is the error I am getting all the time:

( ! ) Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'heman'@'localhost' (using password: YES) in C:\wamp64\www\Bookorama\results.php on line 31

Error! Could not connect to the database.Access denied for user 'heman'@'localhost' (using password: YES) 1045.

I would really appreciate your help. Thanks!

enter image description here

4

There are 4 answers

0
himanshu rathi On

I figured out the problem. I just remembered to put it on this forum, in case it helps someone. The professionals may find it too stupid. So the problem was that my WAMP server has 2 databases, MySQL and MariaDB. And, since MariaDB was using the Port 3306, my PHP code was trying to access MariaDB using mysqli commands. Here's the solution:

  1. Right-click on WAMP icon at the bottom right of the screen.
  2. Go to 'Wamp Settings'.
  3. Remove the check from 'Allow MariaDB'.
  4. Change the port number of MySQL to 3306. (Left click wamp icon -> scroll over MySQL -> Click my.ini -> Press "Ctr + f" and search "port" -> change port number to 3306 at all the 3 places in the file -> Save the file.)
  5. Restart all the services and there you go....!
0
Wallace Stev On

There's a solution that doesn't require deactivating MariaDB and let's you pick among the two whenever you want. Just right click on the WAMP icon, go to "Tools" and click on "Invert default DBMS MariaDB <-> MySQL", that should work. It will set MySQL as a default engine, change the port numbers for you and restart the services all by itself. And if it occurs that you want to switch back to MariaDB just follow the same procedure.

0
Basant Agarwal On

go to this server location C:\wamp64\apps\phpmyadmin5.1.1 and open this file (config.inc.php) this file set some parameters

$cfg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['port'] = 3306; 
0
Dotun Williams On

Go to your Wamp icon, right click, go to tools, then click on "use a port other than 3306 (or whatever your default port might be)". then go back to your browser, Refresh then login with "Root" and no leave the password blank if you haven't even set a password.