udexter
udexter

Reputation: 2347

Fatal error: PDOException SQLSTATE[28000] [1045] Access denied

I'm trying to install a PHP application that was coded by another guy that I can't contact and he used PDO.

I'm getting a lot of problems to run this code. In some areas, MySQL connects well because PDO is not used (this shows that the username and password are right), but in others it throws this exception:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)' in /var/www/x/include/SPDO.php:14 Stack trace: #0 /var/www/x/include/SPDO.php(14): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 /var/www/x/include/SPDO.php(22): SPDO->__construct() #2 /var/www/x/include/class_Projecte.php(55): SPDO::singleton() #3 /var/www/x/dades_proj_edit.php(116): Project->__construct() #4 {main} thrown in /var/www/x/include/SPDO.php on line 14

I don't understand why is trying to look the user www-data. I looked in Google and found a lot of problems with this error, but from a CMS like Magento and nothing that can help me.

Update:

This are the two files used to connect to PDO:

Start of SPDO.php:

<?php
class SPDO extends PDO
{
        private static $instance = null;
        private static $dbhost="localhost";

Upvotes: 2

Views: 37227

Answers (2)

Distdev
Distdev

Reputation: 2312

/var/www/x/include/SPDO.php(14): PDO->_construct('mysql:host=;dbn...', NULL, NULL)

The second and third arguments should be username and password for the database instead of NULLs.

I've checked your files and I've found that instantiation of the configuration is missed (or not included) - username, password, host, etc.

You should use the following code before instantiating of a PDO object:

$config = Config::singleton();
$config->set('dbhost', 'your_hostname'); // Probably localhost
$config->set('dbname', 'your_database_name');
$config->set('dbuser', 'db_username');
$config->set('dbpass', 'db_user_password');

Upvotes: 2

James
James

Reputation: 2669

www-data is the user under which your PHP scripts are being executed. I imagine it is what PDO will use by default if no username is provided for connecting to your database.

It seems that code you have been provided either cannot find the username and password from whatever settings method you use or the developer has not supplied a username and password in the call to PDO correctly.

A temporary fix would be to edit the file "/var/www/x/include/SPDO.php". On line 14 you will find a call to the PDO constructor. Change the second parameter to your username and the third parameter to your password.

Upvotes: 7

Related Questions