Author |
|
laurbest
Joined: 20 Aug 2021 Posts: 2 Location: Iasi
|
Posted: Sat 21 Aug '21 19:27 Post subject: PHP connect Error 1044 |
|
|
Hi!I tried to connect a PHP script to a database(called eu).I checked all privileges for user root,the one I use in PHP ,but I get this error:
Code: |
Array (
[0] => Array (
[errno] => 1044
[sqlstate] => 42000
[error] => Access denied for user ''@'localhost' to database 'eu'
)
)
1
|
Code: |
?php
/* db.php contains these $dbname = "eu";
$dbusername = "root";
$dbpassword = "";
$dbsrvname = "localhost";*/
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
if (!$con){
echo('Connection ERROR');
die(print_r(mysqli_error($con)));
}
$query="USE eu";
$stms=mysqli_query($con,$query);
$x=$_POST['username'];
$y=$_POST['password'];
if ($stms === false){
echo('ERROR during query execution: ');
die(print_r($con->error_list));
}
$row = mysqli_fetch_array($stms, MYSQLI_ASSOC);
if ($row){
die('Logged in');
}
else{
die('Wrong username or password');
}
?>
|
Mod note: added code tags |
|
Back to top |
|
spser
Joined: 29 Aug 2016 Posts: 97
|
Posted: Mon 23 Aug '21 3:11 Post subject: Re: PHP connect Error 1044 |
|
|
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword);
try |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Mon 23 Aug '21 8:58 Post subject: |
|
|
MySQL Error 1044 is access denied for the user.
Either the user password combination is not correct or the hostname in the MySQL user table is a different one than expected. |
|
Back to top |
|
laurbest
Joined: 20 Aug 2021 Posts: 2 Location: Iasi
|
Posted: Tue 24 Aug '21 19:40 Post subject: I looked into user table and it was all fine. |
|
|
James Blond wrote: | MySQL Error 1044 is access denied for the user.
Either the user password combination is not correct or the hostname in the MySQL user table is a different one than expected. |
|
|
Back to top |
|
mraddi
Joined: 27 Jun 2016 Posts: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Wed 25 Aug '21 20:39 Post subject: |
|
|
Good evening,
why is the user's name empty in the error-message?
Code: | Access denied for user ''@'localhost' to database 'eu' |
Normally there should be the username you used for trying to get access to the database.
For debugging-purpose try to output $dbusername before you use mysqli_connect - just to be sure that the variable is set correctly. Do the same for the other variables used in mysqli_connect. |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Thu 26 Aug '21 9:15 Post subject: Re: I looked into user table and it was all fine. |
|
|
laurbest wrote: | James Blond wrote: | MySQL Error 1044 is access denied for the user.
Either the user password combination is not correct or the hostname in the MySQL user table is a different one than expected. |
|
It might be that the user on MySQL is
user @ localhost or user @ % (wildcard)
e.g.
Code: |
MariaDB [mysql]> SELECT user,host FROM user;
+----------+-----------+
| User | Host |
+----------+-----------+
| backup | % |
| root | 127.0.0.1 |
| root | ::1 |
| backup | localhost |
| pma | localhost |
| root | localhost |
+----------+-----------+
7 rows in set (0.001 sec)
MariaDB [mysql]>
|
And your error message shows that there wasn't a user same set. |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Thu 26 Aug '21 9:20 Post subject: |
|
|
Also instead of print_r use var_dump
There is a "1" that might be a boolean true. |
|
Back to top |
|
mraddi
Joined: 27 Jun 2016 Posts: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Thu 26 Aug '21 13:26 Post subject: |
|
|
James Blond wrote: | There is a "1" that might be a boolean true. |
This "1" is the "true"-result of print_r printed by die(...), because print_r was used without the second parameter set to "true"
print_r-documentation: https://www.php.net/manual/de/function.print-r.php
To circumwent this "problem" either add a true as in
Code: | die(print_r(mysqli_error($con), true)); |
or put the print_r or var_dump before the die():
Code: | var_dump(mysqli_error($con));
die(); |
But this is not related to the original problem of not being able to access the database. |
|
Back to top |
|