Jump to content

Archived

This topic is now archived and is closed to further replies.

tomb04

Fatal error: Uncaught exception 'PDOException

Recommended Posts

Hello,

 

I am getting the following error message...

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\xampp\htdocs\Manager.php:23 Stack trace: #0 C:\xampp\htdocs\Manager.php(23): PDO->__construct('mysql:host=loca...', 'root', 'mypassword') #1 C:\xampp\htdocs\Command_BlogIndex.php(9): Manager->__construct() #2 C:\xampp\htdocs\Command.php(14): Command_BlogIndex->doExecute(Object(Request)) #3 C:\xampp\htdocs\Controller.php(38): Command->execute(Object(Request)) #4 C:\xampp\htdocs\Controller.php(14): Controller->handleRequest() #5 C:\xampp\htdocs\index.php(5): Controller::run() #6 {main} thrown in C:\xampp\htdocs\Manager.php on line 23

 

 

When I looked at Manager.php file, line 23 there is no typo error. Line 23 reads...

$pdo = new PDO($database_dsn, $database_login, $database_password);

 

This is straight copy and paste. I am working on sample only. Not a assignment. PDO is enabled, So this has lead me  to think that I have not been able to contect to the mysql db. I am using xampp (see below the version I am using) on Windows 7 64bit which I don't think is the problem. I have attached the some of the files. I think I have identified where I need to change the characters in the database dsn, username and password?

 

Thank you

 

Tomb

 

Apache Friends XAMPP (Basis Package) version 1.7.3 ######

 

  + Apache 2.2.14 (IPV6 enabled)

  + MySQL 5.1.41 (Community Server) with PBXT engine 1.0.09-rc

  + PHP 5.3.1 (PEAR, Mail_Mime, MDB2, Zend)

  + Perl 5.10.1 (Bundle::Apache2, Apache2::Request, Bundle::Apache::ASP, Bundle::Email, Bundle::DBD::mysql, DBD::SQlite, Randy Kobes PPM)

  + XAMPP Control Version 2.5.8 (ApacheFriends Edition)

  + XAMPP CLI Bundle 1.6

  + XAMPP Port Check 1.5

  + XAMPP Security 1.1

  + SQLite 2.8.17

  + SQLite 3.6.20

  + OpenSSL 0.9.8l

  + phpMyAdmin 3.2.4

  + ADOdb v5.10

  + FPDF v1.6

  + Zend Framework 1.9.6 Minimal Package (via PEAR)

  + Mercury Mail Transport System v4.72

  + msmtp 1.4.19 (a sendmail compatible SMTP client)

  + FileZilla FTP Server 0.9.33

  + Webalizer 2.21-02 (with GeoIP lite)

  + apc 3.1.3p1 for PHP

  + eAccelerator 0.9.6-rc1 for PHP

  + Ming 0.4.3 for PHP

  + PDF with pdflib lite v7.0.4p4 for PHP

  + rar 2.0.0-dev for PHP

  + Xdebug 2.0.6-dev for PHP

  + libapreq2 v2.12 (mod_apreq2) for Apache

 

 

[attachment deleted by admin]

Share this post


Link to post
Share on other sites

It's failing to connect to the database.  Check that the username and password as configured actually work.

Share this post


Link to post
Share on other sites

gizmola,

 

Do you mean through config.inc.php? I have already configured the username and password when I was reading through Peachpit Press PHP 6 and MySQL 5 for Dynamic Web Sites (which worked all well). If else were can you explain where and how?

 

Thank you

 

 

Share this post


Link to post
Share on other sites

Take a look on Rasmus' Toys page,

http://toys.lerdorf.com/archives/50-Using-pecloauth-to-post-to-Twitter.html

 

 

specifically, the fatal error method, PDO throws exceptions when it fails to connect. You need to use try-catch to catch the pdo exceptions thrown.

 

 

ie

try
{
  // new PDO('connect to db');
}
catch(PDOException $e)
{
   // handle error 
   // die($e->getmessage());
}

Share this post


Link to post
Share on other sites

gizmola,

 

Do you mean through config.inc.php? I have already configured the username and password when I was reading through Peachpit Press PHP 6 and MySQL 5 for Dynamic Web Sites (which worked all well). If else were can you explain where and how?

 

 

 

I don't have enough information to tell you that.  What I can tell you is that the source of your problem is stated clearly in the exception stack:

 

PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)'

 

So apparently your configuration is attempting to connect as the mysql user 'root' using some password.  In all likelyhood, the password is wrong.  In general it's best to have a user specifically for the database needed by the application, but that's more an aside as it is irrelevant to the problem you are currently having. 

 

I would diagnose this using the mysql command line client in a shell, or phpMyAdmin.  Regardless, this is why I stated that its's a username password issue.  The only other issue would be the host, if for some reason the mysql server was on another machine, but I'm going to assume that is not your problem.

 

 

Share this post


Link to post
Share on other sites

Take a look on Rasmus' Toys page,

http://toys.lerdorf.com/archives/50-Using-pecloauth-to-post-to-Twitter.html

 

 

specifically, the fatal error method, PDO throws exceptions when it fails to connect. You need to use try-catch to catch the pdo exceptions thrown.

 

 

ie

try
{
  // new PDO('connect to db');
}
catch(PDOException $e)
{
   // handle error 
   // die($e->getmessage());
}

 

Yes that's great that he can catch the exception, only to find that the page is broken because no queries work.  Of course he could also print out the exception which would ultimately lead back to the real problem which is that the username/password are being rejected.

Share this post


Link to post
Share on other sites

The only other issue would be the host, if for some reason the mysql server was on another machine, but I'm going to assume that is not your problem.

 

And error message would be different then.

 

 

Share this post


Link to post
Share on other sites

The only other issue would be the host, if for some reason the mysql server was on another machine, but I'm going to assume that is not your problem.

 

And error message would be different then.

 

 

Not necessarily.  If his target server should be:  somehost.com, yet there's still a mysql server running on the localhost, then the error would still indicate that the root user password was wrong.

Share this post


Link to post
Share on other sites

In this case, yes.

Share this post


Link to post
Share on other sites

For some reason the password has changed now I have to figure how reset password to do this. Can this be done. The password saved in config.inc.php is not the one.

 

I went through cmd (C:\xampp\mysql\bin\mysql -u root -p). To find out about the password.

I wonder if the following has anything to do it in the phpadmin (I don't think so)...

 

Connection for controluser as defined in your configuration failed.

 

 

Share this post


Link to post
Share on other sites

Actually I had the wrong password typed so pls disregard my previous notes. However I am having the following error...

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'ODBC'@'localhost' (using password: NO)' in C:\xampp\htdocs\Manager.php:17 Stack trace: #0 C:\xampp\htdocs\Manager.php(17): PDO->__construct('mysql:host=loca...', NULL, NULL) #1 C:\xampp\htdocs\Command_BlogIndex.php(9): Manager->__construct() #2 C:\xampp\htdocs\Command.php(14): Command_BlogIndex->doExecute(Object(Request)) #3 C:\xampp\htdocs\Controller.php(38): Command->execute(Object(Request)) #4 C:\xampp\htdocs\Controller.php(14): Controller->handleRequest() #5 C:\xampp\htdocs\index.php(5): Controller::run() #6 {main} thrown in C:\xampp\htdocs\Manager.php on line 17

 

 

Share this post


Link to post
Share on other sites

Now you're trying to login without username and password set.

Share this post


Link to post
Share on other sites

So what you saying I got get and set wrong. Manager.php  The syntax is reading...

function __construct()

  {

    $database_dsn = Registry::get("database_dsn");

$database_login = Registry::get("database_login");

$databse_password = Registry::get("database_password");

 

$pdo = new PDO($database_dsn, $databse_user, $database_password);

self::$DB = $pdo;

 

self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXECPTION);

  }

 

  function prepareStatment($stmt_s)

  {

    if(isset(self::$stmts[$stmt_s]))

{

  return self::$stmts[stmt_s];

}

$stmt_handle = self::$DB->perpare($stmt_s);

self::$stmts[$stmt_s] = $stmt_handle;

return $stmt_handle;

}

and in the controller.php the sytax is read...

function init()

  {

    Registry::set("database_dsn", "mysql:host=localhost;dbname=messages");

    Registry::set("database_login", "root");

    Registry::set("database_password", "thepassword");

  }

 

Share this post


Link to post
Share on other sites

Check if you're getting anything from registry

 

function __construct()
  {
    echo $database_dsn = Registry::get("database_dsn");
   echo $database_login = Registry::get("database_login");
   echo $databse_password = Registry::get("database_password");
   
   $pdo = new PDO($database_dsn, $databse_user, $database_password);
   self::$DB = $pdo;
   
   self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }

 

This should echo your database details. If it doesn't something's wrong with either your Registry() class, or init() is not called early enough to set this data for PDO

Share this post


Link to post
Share on other sites

In your code you have

$database_login = Registry::get("database_login");
$databse_password = Registry::get("database_password");

 

Note that later you use the variables $database_user and $database_password

Share this post


Link to post
Share on other sites

Request.php is showing blank page. I have created tables fields are id, title, message, date_added. But no there is no data in the field.  So you reckon there is something wrong with Registry() class.

 

class Registry

{

  private static $values = array();

    static function set($key, $val)

  {

    self::$values[$key] = $val;

  }

static function get($key)

  {

    if(isset(self::$values[$key]))

    {

      return self::$values[$key];

    }

    return null;

  }

 

}

 

 

I have ammended typo errors and the error is now showing...

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1049] Unknown database 'messages'' in C:\xampp\htdocs\Manager.php:17 Stack trace: #0 C:\xampp\htdocs\Manager.php(17): PDO->__construct('mysql:host=loca...', 'root', 'thepassword') #1 C:\xampp\htdocs\Command_BlogIndex.php(9): Manager->__construct() #2 C:\xampp\htdocs\Command.php(14): Command_BlogIndex->doExecute(Object(Request)) #3 C:\xampp\htdocs\Controller.php(38): Command->execute(Object(Request)) #4 C:\xampp\htdocs\Controller.php(14): Controller->handleRequest() #5 C:\xampp\htdocs\index.php(5): Controller::run() #6 {main} thrown in C:\xampp\htdocs\Manager.php on line 17

 

Unknown database 'messages'' -- Is it saying that there is no database called messages? When I have created messages table in phpmyadmin.

 

The sample I am working on is based on linux. Has this got anything to do with it.

 

Share this post


Link to post
Share on other sites

database != table

When you go to phpMyAdmin's main page you can click 'databases' tab on top, to see databases created on your server. Tables are stored within databases.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.