0

I am developing a login/register system for a website but when i'm trying to connect to the database it gives this error:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\register.php on line 17

I'm using a external config file wich is been imported.

My config file:

// Connection details
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";

My connection code:

// Create connection
   $conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

I found one solution that worked to connect but then my config file was useless.

No config but only this:

// Create connection
   $conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
// Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

If someone nows a solution where i can keep using my config file please post it here.

Thanks in advance.

Jamie
  • 91
  • 1
  • 12
  • Access denied: your username and/or password are wrong, or your mySQL server not allow that user from localhost (strange...), or your have to configure your mySQL server – fusion3k Feb 07 '16 at 18:19
  • if your 2nd solution works, you can change in this sense config file! $password='password', $dbname='database'. Side note: password='password' don't seem the very best, also connecting as 'root'... – fusion3k Feb 07 '16 at 18:22
  • @fusion3k my account/password are OK, i don't know if my mysql server does, i did'nt configured my mysql server. – Jamie Feb 07 '16 at 18:23
  • @fusionk3 i'm not going to show you my password on this site! – Jamie Feb 07 '16 at 18:24
  • Do you have phpMyAdmin? – fusion3k Feb 07 '16 at 18:24
  • @fusionk3 yes i do why? – Jamie Feb 07 '16 at 18:24
  • well done! (referring to password) So, change the working password in config file. Otherwise, you can add/change users and permissions in phpMyAdmin: [see more](https://wiki.phpmyadmin.net/pma/user_management) – fusion3k Feb 07 '16 at 18:26
  • @fusionk3 how do you mean – Jamie Feb 07 '16 at 18:29
  • @fusionk3 can you invite me for chat? – Jamie Feb 07 '16 at 18:30
  • you say: "I found one solution that worked", then you can add these values to your config file. Ok? – fusion3k Feb 07 '16 at 18:45
  • Are you actually including the config file properly? Can you post more of the code to show the inclusion of the file, and then the attempt to connect using the variables inside that config? – Source Matters Feb 07 '16 at 18:50
  • Your server seems to have differences credentials for root@localhost and for root@127.0.0.1, or you're not including properly your credentials file properly. Try to print the credentials just before connecting, and confirm that they're what you configured. If that's the case, then you might have to check your server to see if you have different users. – Nuno Pereira Feb 07 '16 at 18:55
  • @fusion3k i have them in my config The config is included with require(/classes/config.class.php); i Will Try – Jamie Feb 07 '16 at 19:07
  • @SourceMatters iT is included with require – Jamie Feb 07 '16 at 19:08
  • @NunoPereira how do you mean i Will Try to print – Jamie Feb 07 '16 at 19:08
  • echo "sn=$servername, u=$username, p=$password, db=$dbname;"); // then connect and so – Nuno Pereira Feb 07 '16 at 19:12

1 Answers1

0

Well, if the connection works with this values:

$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');

But not with this values:

$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "cloud";

Why don't you just set the "working" values into your config file. So it would look like this:

$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";

Another solution which is described here includes the creation of a new user, so you don't connect via root, but the new created user.

Community
  • 1
  • 1
MikeVe
  • 1,062
  • 8
  • 13