1

I am trying to write a php script which displays a database on a website and I can't figure out why it will only accept the root user as a login. If I put any other user in the script it will just say access denied.

The user I am trying to use has all privileges granted on that database.

+-----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for user@localhost                                                                                                              |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY PASSWORD '' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `my_db`.* TO 'user'@'localhost' WITH GRANT OPTION                                                            |
+-----------------------------------------------------------------------------------------------------------------------------------------+
<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "my_db";

$mysqli = new mysqli( $servername, $username, $password, $dbname );

if ($mysqli->connect_errno){
    printf("connect failed: %s\n", $mysqli->connect_errno);
    exit();
}

$rs = $mysqli->query( "SELECT * FROM table ORDER BY device_name ASC;" );
Dharman
  • 30,962
  • 25
  • 85
  • 135
thatoneguy
  • 13
  • 2
  • Are you sure you are providing the correct password, when trying to connect with other users? – Lucas Arbex Oct 07 '19 at 19:35
  • yes I have checked that many times and have used multiple different accounts with separate passwords – thatoneguy Oct 07 '19 at 19:40
  • What's the output of your `printf("connect failed: %s\n", $mysqli->connect_errno);` line? If you match up the error number with the [reference page](https://dev.mysql.com/doc/refman/5.6/en/server-error-reference.html), it should tell you what your error is. – Joundill Oct 07 '19 at 19:53
  • @Joundill Why would you do that? Just enable error reporting: [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 07 '19 at 19:54
  • 1
    @Dharman because it requires 0 code changes to work out what the error is. – Joundill Oct 07 '19 at 19:55
  • its just a standard 1045 error: Access denied for user '%s'@'%s' (using password: %s) – thatoneguy Oct 07 '19 at 23:06

0 Answers0