Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
My sql database connection

  • Hello, 
    I ve just started using  FuelPHP ...have trouble to connect to my database.
    I use My sql db.
    Below is what i got.

    Please, if someone can help me.
    Thanks

    Error!

    Fuel\Core\Database_Exception [ Error ]:
    invalid data source name

    C:/xampp/htdocs/fuelphp/fuel/core/classes/database/pdo/connection.php @ line 112

  • HarroHarro
    Accepted Answer
    Your configuration is wrong, but you'd probably already figured that. ;)

    When it comes to configuration, Fuel works with environments, which are defined as folders within the config directory. By default, Fuel runs in the "development" environment.

    All config that is environment (server, webserver, etc) specific should go into an environment folder, and that includes database configuration.

    If you have a configuration file in both your environment folder and in the global config folder, like is the case for db.php, both will be merged, where the environment one wins. The rationale behind this is that you can use the global file for global config, and the environment one for environment specific config.

    An example for this is the main config file: you would have profiling switched of in that file, but you would have a config.php in the development folder that enables profiling. So it's on when you develop, and off in other environments such as staging and production.

    The most common beginners mistake is that configuration is added to the global config (in this case APPPATH/config/db.php instead of in the environment config (in this case APPPATH/config/development./db.php).  Because of the merge operation mentioned above, the empty environment config will overwrite the global one you've made.

    If this is not the case, please post your APPPATH/config/development/db.php (wipe passwords and hostnames), so we can have a look.
  • Thank you Harro, 
    I am completely new in Fuelphp.. just fallowed the instructions for the main web site...
    Anyway, i first put this code below in global db.php, that could not work...after put in the development/ db.php and after in every envirenment db php.

    This is code:
    <?php
    /**
     * The development database settings. These get merged with the global settings.
     */

    return array(
       'default' => array(
           'connection' => array(
           'hostname' => 'localhost',
           'port' => '3306',
           'database' => 'my database',
           'username' => 'myusername',
           'password' => 'mypassword',
           'persistent' => false,
           'compress'   => true,
           ),
        ),
    );
  • HarroHarro
    Accepted Answer
    There are two different ways of defining the database, and that depends on the type. There is one for legacy drivers, and there is one for PDO.

    Since your config doesn't specify the type, Fuel will default to PDO, but you have used the legacy syntax.

    return array(
        'active' => 'pdoconfig',

        // use this for legacy drivers such as mysql or mysqli
        'legacyconfig' => array(
            'type'        => 'mysqli',
            'connection'    => array(
                'hostname'   => 'localhost',
                'database' => 'my database',
                'username' => 'myusername',
                'password' => 'mypassword',
                'persistent' => false,
            ),
            'identifier'   => '`',
            'table_prefix' => '',
            'charset'      => 'utf8',
            'collation'    => 'utf8_general_ci',
            'enable_cache' => true,
            'profiling'    => true,
        ),

        // use this when you use PDO
        'pdoconfig' => array(
            'type'        => 'pdo',
            'connection'    => array(
                'dsn'   => 'mysql:host=localhost;dbname=my database',
                'username'   => 'my username',
                'password'   => 'my password',
                'persistent' => false,
            ),
            'identifier'   => '`',
            'table_prefix' => '',
            'charset'      => 'utf8',
            'collation'    => 'utf8_general_ci',
            'enable_cache' => true,
            'profiling'    => true,
        ),

    So it probably fails because it defaults to PDO but the "dsn" entry is missing. It is required because it specifies the database engine to connect to (mysql in this example).  Alternatively use your existing config, but add: "type" => "mysqli".
  • Harro, thank you lots...

    I also found that incorrect after i was reading the documentation ...but now i got like this below , even i know my database, username, password are correct.

    Oh, excuse me for a ton of questions.
    I am going to continue reading about, to resolve bymyself, but all suggestions are welcome .

    Thanks again.

    1045!
    Fuel\Core\Database_Exception [ 1045 ]:
    Access denied for user 'mydatabaseusername'@'localhost' (using password: YES)

    C:/xampp/htdocs/fuelphp/fuel/core/classes/database/mysql/connection.php @ line 108

    103 catch (\ErrorException $e)
    104 {
    105 // No connection exists
    106 $this->_connection = null;
    107
    108 throw new \Database_Exception(str_replace($password, str_repeat('*', 10), mysql_error()), mysql_errno());
    109 }
    110
    111 // \xFF is a better delimiter, but the PHP driver uses underscore
    112 $this->_connection_id = sha1($hostname.'_'.$username.'_'.$password);



  • HarroHarro
    Accepted Answer
    The message is pretty clear I'd say.

    The username and password you have specified don't give you access to that database, so you need to check your MySQL config. Make sure username and password are ok, and that this user has rights to access this database from localhost.

    And when you make changes, don't forget to flush privileges to make MySQL reload the permissions.
  • Thanks Harro...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion