开发者

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

开发者 https://www.devze.com 2023-02-08 06:51 出处:网络
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 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.

  • PHP version: PHP 5.3.3-7
  • PDO Driver for MySQL 5.1.49
  • Apache version: Apache/2.2.16 (Debian)
  • Apache modules: core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_reqtimeout mod_setenvif mod_status

Update:

This are the two files used to connect to PDO:

  • SPDO.php: http://pastebin.com/utCjresv
  • class_Config.php: http://pastebin.com/6CENLDf3

Start of SPDO.php:

<?php
class SPDO extends PDO
{
       开发者_如何学Python private static $instance = null;
        private static $dbhost="localhost";


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.


/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');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号