I recently moved a script I created for Google Maps over from my development site and I changed the information in the mysqli line and now I'm recieving an error mesage -- any idea what's going on here? Here's the error message:

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'Username'@'localhost' (using password: YES) in /home/cigardig/public_html/XXXX.php on line 259

Here's the relevant code:

$mysqli = new mysqli(  "localhost", "[I]Username[/I]", "[I]Password[/I]", "[I]DB Name[/I]" );

I also don't know if this has anything to do with it but this script is using stored functions which grant as follows:

/*!50003 CREATE DEFINER=`root`@`localhost` FUNCTION `BlahBlah`(Lat1 FLOAT, Long1 FLOAT, Lat2 FLOAT, Long2 FLOAT) RETURNS float [........]

Dani AI

Generated

Authentication failures like the one shown here usually fall into three buckets: credential parsing (special characters), host/account mismatches, or insufficient grants. ’s discovery and the troubleshooting suggestions from and are on point. The short checklist and the safer connection pattern below make these problems easier to spot and prevent.

  • Confirm which MySQL account is actually used after connection: run SELECT CURRENT_USER(); from the connection that fails and compare with the account in SHOW GRANTS FOR 'username'@'host';. MySQL accounts are host-specific (for example 'user'@'localhost' is different from 'user'@'%' or 'user'@'127.0.0.1').
  • Avoid quoting/parsing traps: prefer single-quoted PHP literals for raw passwords or read credentials from a config file or environment variables so shell-like characters (, $, \) are not accidentally interpreted.
  • Socket vs TCP: try 127.0.0.1 instead of localhost if the server uses different connection types.
  • If stored routines exist with a DEFINER clause, inspect them with SHOW CREATE FUNCTION func_name; or query INFORMATION_SCHEMA.ROUTINES for ROUTINE_DEFINER and ensure the connecting account has the needed privileges.

A simple, more robust pattern is to keep creds out of code and use a modern DB API with exceptions enabled. Example using PDO:

$dsn = 'mysql:host=127.0.0.1;dbname=yourdb;charset=utf8mb4';
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$db = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASS'), $options);

Limit privileges (avoid root), verify grants, and store credentials outside the webroot. The checks above reproduce the diagnostic steps that helped here and will make similar problems easier to find in future.

Recommended Answers

All 10 Replies

I forgot to mention in the previous post, the only thing that has changed was the username, password, and DB selected between the code. When I change it back to the old values it works but when I try the new ones it stops working again and generates the message.

You have changed old database credentials to new.
Check your new database's credentials proper.I think it should not be username:root for host:localhost.
Something went wrong with credentials only.

The username/password combo is correct?

Member Avatar for Member #120589

May very well be you SP. Can you access DB w/out SP?

May very well be you SP. Can you access DB w/out SP?

I can access it through PHPmyAdmin, Remote mySQL, as well as as on other pages - is that what you're talking about?

Member Avatar for Member #120589

php.net gives this example:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

I realise you don't want to post your actual settings, but do they match this format? They seem to from your post.

I think a typical default setting could be:

$mysqli = new mysqli('localhost', 'root', '', 'yourdbname');

phpmyadmin doesn't depend on you specifying a host, so it may be down to your host details. I assume your username and pw will be correct if you can use them to log in to phpmyadmin.

Perhaps it's the db name? Check spelling closely - no spaces. Underscores or dashes could be confused?

commented: Solved it! +3

php.net gives this example:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

I realise you don't want to post your actual settings, but do they match this format? They seem to from your post.

I think a typical default setting could be:

$mysqli = new mysqli('localhost', 'root', '', 'yourdbname');

phpmyadmin doesn't depend on you specifying a host, so it may be down to your host details. I assume your username and pw will be correct if you can use them to log in to phpmyadmin.

Perhaps it's the db name? Check spelling closely - no spaces. Underscores or dashes could be confused?

Ugh, I feel like an idiot! I was using double quote instead of single quotes, changing that fixed and it's working beautifully now! :$

Member Avatar for Member #120589

Hi j. I have no idea wny this should happen. I tried it with both just now and both worked file. Perhaps you had a $ or \ or something weird in one of the strings?

Anyway, something I'll look out for. Thanks.

Hi j. I have no idea wny this should happen. I tried it with both just now and both worked file. Perhaps you had a $ or \ or something weird in one of the strings?

Anyway, something I'll look out for. Thanks.

ardav, you are right on. I had a $ in the password and it was parsing it weird as a result :)

Member Avatar for Member #120589

Ahh. It's so good to be me sometimes... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.