PHP Classes

Cannot connect to DB

Recommend this page to a friend!

      PHP MySQL Shell  >  All threads  >  Cannot connect to DB  >  (Un) Subscribe thread alerts  
Subject:Cannot connect to DB
Summary:Access denied
Messages:6
Author:Carlo Glinetzki
Date:2019-08-15 08:41:10
 

  1. Cannot connect to DB   Reply   Report abuse  
Picture of Carlo Glinetzki Carlo Glinetzki - 2019-08-15 08:41:10
Hi, I want to use your class, but it does not work for me.
I always got access denied, but I'm quite sure that my credentials are correct, because I can use mysql client on linux shell.
May it have to do with my Versions?
I have PHP version 7.2.19-0ubuntu0.18.04.2 and 10.3.17-MariaDB.

  2. Re: Cannot connect to DB   Reply   Report abuse  
Picture of Dev Sharma Dev Sharma - 2019-08-15 09:09:51 - In reply to message 1 from Carlo Glinetzki
Hi, I want to use your class, but it does not work for me.
I always got access denied, but I'm quite sure that my credentials are correct, because I can use mysql client on linux shell.
May it have to do with my Versions?
I have PHP version 7.2.19-0ubuntu0.18.04.2 and 10.3.17-MariaDB.

Hi Carlo,

thank you for your interest in my class and I am here to guide you. In example.php we have this line:

"$db = new Shell('localhost', 'username', 'pass', 'database name', 3306);"

Make sure every credential there is correct, username, password, database and the port. The hostname is "localhost" so make sure your host or your computer uses localhost, if not change it to something else. Also if you have access to your terminal try this command:

mysql -u root -p

it will ask you for your password and type your password there, if it doesn't work then it means the password is incorrect then you will have to reset it but if it does, then I suggest you edit the shell.class.php and find the line:

"if ($this->db->connect_errno) die('Error: Unable to connect to Database.');"

then change it to:

"if ($this->db->connect_errno) die($this->db->connect_errno);" (without the double quotes)

Let me know what error it spills out, but before doing this please do make sure to try the above steps I mentioned above.

Thanks,

Dev.

  3. Re: Cannot connect to DB   Reply   Report abuse  
Picture of Carlo Glinetzki Carlo Glinetzki - 2019-08-15 11:35:24 - In reply to message 2 from Dev Sharma
Hi Dev,

thanks for the fast answer, but exactly what you proposed me, I have done already ;)
I can use mysql on cli in linux, works correctly, my creadentials are 100% correct and still it throws me error 1045.
That's why I thought it may be an issue of the versions

  4. Re: Cannot connect to DB   Reply   Report abuse  
Picture of Dev Sharma Dev Sharma - 2019-08-16 08:54:52 - In reply to message 3 from Carlo Glinetzki
It does seem like a weird issue but yes maybe it's your PHP version because I tested this with PHP 7.1.28 in localhost and it worked connecting with MySQL.

Another issue is that if you provide an invalid database name, it will spill out that error because this script requires a valid database to connect to in order to execute queries.

  5. Re: Cannot connect to DB   Reply   Report abuse  
Picture of Timo Henke Timo Henke - 2019-08-16 18:02:30 - In reply to message 3 from Carlo Glinetzki
normaly using mysql on the console will connect using a local socket or to 127.0.0.1.

If the console will connect but PHP will not, it could be of different methods (localhost is NOT 127.0.0.1 from mysql perspective).

Maybe try to set the ip (127.0.0.1) instead of localhost.

No idea about mysqli - i am using PDO for years now.

  6. Re: Cannot connect to DB   Reply   Report abuse  
Picture of Carlo Glinetzki Carlo Glinetzki - 2019-08-17 05:36:04 - In reply to message 4 from Dev Sharma
Hi Dev,

it is not the dbname, and it is also not a problem of IP, because I tried also to change localhost to IP. I am using on the same server scripts which works fine, the only difference I can see is, on these scripts I am using mysqli_connect and your class uses simply msqli. I have not much knowledge about php, and I don't know what is the difference of those.
Furthermore I found out that mysql is writing an errorlog under /var/log/mysql, and there it says:
2019-08-17 10:04:42 9 [Warning] Access denied for user 'root'@'localhost' (using password: NO)
So I thought it may be a problem of the user and tried a normal user, but same message:
2019-08-17 10:19:19 14 [Warning] Access denied for user 'waree'@'localhost' (using password: NO)
That means the password seems not to be passed to the login process, because the username is passed correctly.
I then modified your class and echoed all the parameters just before the call of mysqli, and they show up all correctly. Now I have no idea anymore, maybe it is really an issue with mariadb in combination with mysqli, and I will stop now searching now, because it is not important enough to loose too much time ;)
By the way, I set up another virtual system with php 5.6 and latest mysqldb, and there it is working fine.
If you or someone else will find out more, you can let me know ;)