来自php脚本的MySQL数据库连接错误通过bash脚本运行

I currently have a bash script with the following code to simply execute a PHP script (via a cron job):

php path/to/my/file.php

That PHP script then does a bunch of random stuff. One of which is connect to a MySQL database using a config file and then running SQL statements to alter a database. However, I keep getting errors like:

PHP Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in /opt/local/www/example.com/assets/classes/Importer.php on line 109
PHP Warning:  mysql_query(): A link to the server could not be established in /opt/local/www/example.com/assets/classes/Importer.php on line 109

The bash script and PHP files are all stored on the same server. Is there a reason my PHP script being run via a bash command wouldn't be connecting to the database despite the fact that going to the same file via a browser successfully connects to the database?

The weird part is that the error is correctly grabbing 'localhost' as the server name from the config file but for some reason isn't doing the same with the database username and password stored in there.

EDIT: Added code below

This code below is run from file.php mentioned above.

    public function __construct()
{
    // We need to manually include these classes since they are being run from a bash script
    require_once('core.php');
}

core.php (mentioned in the above code block) then has the following code:

// Include environment-specific configuration file
require_once 'config.php';

// TODO: Move this to Core::getDatabase();
$database = new Database(Core::getApplication()->getDbHost(), Core::getApplication()->getDbUser(), Core::getApplication()->getDbPass(), Core::getApplication()->getDbName());

config.php is where the 'localhost' var and database username and password are coming from

Edit #2: I don't believe it's a file path issue because the correct files are being grabbed from the config and made in the mysql_connect() call as confirmed by outputting them right before that call. It seems like it's connecting to the database in one file and then forgetting about it or something when it comes time to run the queries...?

Edit #3: I think I figured it out. The destructor for the Database class was being called and was disconnecting from the database. I commented out the disconnect and now it appears to be working. I'll have to figure out why that's being called.

It's probably an issue with current directory. It's probably looking for the config file in the current directory, and that directory is different when you run it via bash.

You are probably doing:

require('config.php');

And assuming that config.php is in the same directory - but that's not how it works, it looks in the current directory, not the same directory as the php file.

Instead use the __FILE__ define to figure out where you are and get an absolute path based on that. Something like this:

require(dirname(__FILE__) . 'config.php');

Figure out why the username is coming out empty (how about sharing some code?). That'll almost certainly fix the login problem. You pretty much guaranteed don't have a "Nousername@localhost" account in MySQL, so it's properly denying you access.