FEED-BABE
FEED-BABE

Reputation: 17

mysqli_connect fails with dbpassword

My system seems to have some weird problems I can't explain. I have a following code snip that I would use to connect to and read data from the database

define('DB','products');
define('HOST','localhost');
define('DBPASS','password');
define('DBUSER','root');
define('DBTABLE','product_tb');
$conn=mysqli_connect(HOST,DBUSER,DBPASS) or die('Could not connect: ' . mysqli_error($conn));

But it always reports an error that I can't make a connection with mysqli_connect. I try running this code on another system and it works fine. Here are the two warning and an failed connection error.

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in D:\xampp\htdocs\products.php on line 7

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in D:\xampp\htdocs\products.php on line 7 Could not connect:

This happens when I test system doesn't require password, so I left the DBPASS as blank but once I use this on the real system, it fails with the exact same error, no matter whether I left out the DBPASS or not.... I thought it was cache but I restarted mysql and apache but nothing works then. Thank you if you could offer some help.

Upvotes: 0

Views: 14760

Answers (2)

kawerewagaba
kawerewagaba

Reputation: 1357

I found this after a very long hustle - it actually lay right in my cPanel->Home->MySQL Databases:

Remote MySQL Host

“mysql2000.my-virtual-panel.com” is the address for the MySQL server. When you connect to the MySQL server, you must specify this host.

$host="mysql2000.my-virtual-panel.com";

It should help.

Upvotes: 0

bumperbox
bumperbox

Reputation: 10214

To check for connection errors you should use mysqli_connect_error() not mysqli_error()

That will solve one of your error messages.

$conn=mysqli_connect(HOST,DBUSER,DBPASS) or die('Could not connect: ' . mysqli_connect_error());

The other error message is caused by invalid privileges in the database server. Stopping you from accessing the database. Like @Dragon commented, can you login with the username/password combo from the cmdline. If you don't have cmd line access, do you have php myadmin or something else, that you can check the user/pass/db combo with to make sure it works.

Also as @MarcB says, it would be wise to use a login that only has access to a specific database on your server rather then root access to all databases. That way if you code got hacked, they would only have access to the 1 database (in theory).

Upvotes: 2

Related Questions