0

I try to realize a CMS in MVC with OOP PHP.

I use a Unix System (Ubuntu 14) and LAMPP (XAMPP) as Server.

In my config.php I fetch the Information for a Database Connection from a XML file.

I try this Methods before to get a MySql Connection with PDO in PHP, and it works.

There are two things I do different in this case is, the first is, that i fetch the MySql Connection Information from a XML file. The second is that i work on an Unix System.

Stackoverflow marked this Questions as a duplicate, but I cant't get forward with that Answer. Maybee you can take a look, and explain it for a newbie ;)

MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

config.php:

final class Config {

/**************************************************************************/

/*PRIVATE FUNCTION*/

/*
 * GET XML CONTENT
 */
private static function getXmlContent($file){
    //set xml file
    $xmlFile    = URL.$file;
    //get content
    $xmlContent = simplexml_load_string(file_get_contents($xmlFile));
    //json encode
    $xmlJSON    = json_encode($xmlContent);
    //json decode to assoc array
    $xmlArr     = json_decode($xmlJSON, true);

    return $xmlArr;
}

/**************************************************************************/

/*PUBLIC FUNCTION*/

/*
 * MYSQL
 * @return  $mySql      (array, assoc)
 */
public static function db(){
    $database       = Config::getXmlContent('database.xml');

    $mySql = array(
        'type'      => (string) $database['type'],
        'host'      => (string) $database['host'],
        'database'  => (string) $database['database'],
        'port'      => (string) $database['port'],
        'socket'    => (string) $database['socket'],
        'user'      => (string) $database['user'],
        'password'  => (string) $database['password'],
        'prefix'    => (string) $database['prefix']
    );
    //return
    return (array) $mySql;
}

/*
 * PDO
 * @return  $pdo        (array, assoc)
 */
public static function pdo(){
    $pdo = array(
        PDO::ATTR_PERSISTENT            => true,
        PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC
    );
    //return
    return (array) $pdo;
}
}

database.xml:

<?xml version="1.0" encoding="UTF-8"?>
<sql>
<type>mysql</type>
<host>localhost</host>
<database>database</database>
<port>3306</port>
<socket></socket>
<user>user</user>
<password>password</password>
<prefix>prefix_</prefix>

lib/model.php:

class Model {

//CONSTRUCT
function __construct(){
    //init database
    $this->database = new Database(Config::db(), Config::pdo());
}
}

lib/database.php:

class Database extends PDO {

/*
 * CONSTRUCT
 * 
 * @param   $sql        (array)
 *          $pdo        (array)
 * 
 * stackoverflow:
 * https://stackoverflow.com/questions/11622317/error-on-my-pdo-construct-php
 * https://stackoverflow.com/questions/19582469/uncaught-exception-pdoexception-with-message
 * 
 * phpManuel:
 * http://php.net/manual/de/pdo.construct.php
 */
public function __construct($sql, $pdo) {
    /*
     * PARENT CONSTRUCT
     * 
     * @param   $dsn            (string)
     *          $username       (string)
     *          $passwd         (string)
     *          $options        (array)
     */
    parent::__construct($sql['type'].':host='.$sql['host'].';port='.$sql['port'].';dbname='.$sql['database'], $sql['user'], $sql['password'], $pdo);
}
}

The PHP Error Message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'user'@'localhost' (using password: YES)' in /opt/lampp/htdocs/xxx/application/lib/Database.php:37 Stack trace: #0 /opt/lampp/htdocs/xxx/application/lib/Database.php(37): PDO->__construct('mysql:host=loca...', 'user', 'password', Array) #1 /opt/lampp/htdocs/xxx/application/lib/Model.php(18): Database->__construct(Array, Array) #2 /opt/lampp/htdocs/xxx/application/lib/Controller.php(19): Model->__construct() #3 /opt/lampp/htdocs/xxx/application/control/index.php(13): Controller->__construct() #4 /opt/lampp/htdocs/xxx/application/lib/Application.php(61): Index->__construct() #5 /opt/lampp/htdocs/xxx/application/lib/Application.php(168): Application->loadDefaultController() #6 /opt/lampp/htdocs/xxx/index.php(40): Application->init() #7 {main} thrown in /opt/lampp/htdocs/xxx/application/lib/Database.php on line 37

I'm new on Unix and using the Terminal, so please write down a little description if I have to use it.

I hope you can help me as quick as you can.

Thank You!

Community
  • 1
  • 1

1 Answers1

0

First, check your database credentials for user in your config. If you will not find anything with it, try to log your $sql array before pass it to parent::__construct() and check is it any errors with user credentials here.

  • There's no problem with fetch the credentials from the xml file. A var_dump works. I check the credentials, and i also try it with 'root' & 'root' as user & password. Doesn't work too... – John in a Million Jun 02 '15 at 23:48