Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Frustrating problems with connecting to the database
  • I have spent quite a bit of time trying to figure out what is going on with connecting to the database in fuelPHP. I have tried MySql and PDO. Neither work in the app but strangely enough both work just fine when running oil migrations. When I use the PDO option, I get the following: Fuel\Core\Database_Exception [ Error ]: invalid data source name When I use the MySql option, I get the following: Fuel\Core\Database_Exception [ 1046 ]: No database selected I have tried everything. The database exists and the connection info is correct (I assume since Oil has no problem using it to migrate). Here is how it looks like: For PDO: return array(
    'default' => array(
    'connection' => array(
    'dsn' => 'mysql:host=localhost;dbname=blog',
    'username' => 'username',
    'password' => 'password',
    'persistent' => false,
    ),
    ), For MySql: return array(
    'default' => array(
    'connection' => array(
    'hostname' => 'localhost',
    'port' => '3306',
    'database' => 'blog',
    'username' => 'username',
    'password' => 'password',
    'persistent' => false,
    ),
    ), Can anyone shed some light on what's going on here? I'm on Mac OSX Lion.
  • This got me with unit testing. The environment gets set to Fuel::TEST automatically and hence no DB! Its awesome though because I can use a duplicate DB for my unit tests which I would normally do anyways, but this post saved me a lot of time.
  • The idea behind this is that you can run tests against a known (or perhaps on the fly created) database, without impacting your development or production database environment.
  • OMG. After a bit more digging around I found out what the problem was. I was setting the server environment variable incorrectly so the appropriate config was not being read. It's not entirely clear from the docs how to actually set the server environment variables. After a bit of testing here is how it should be set for Apache web server: SetEnv FUEL_ENV DEVELOPMENT Hope this helps someone.
  • Harro Verton wrote on Tuesday 31st of July 2012:

    Hi, for me it just worked if I set on apache: SetEnv FUEL_ENV test with test lowcase and not upcase (like it says on http://docs.fuelphp.com/general/environments.html) A bit of code search.. made me understand why (bootstrap.php - line 30): Fuel::$env = (isset($_SERVER) ? $_SERVER : Fuel::DEVELOPMENT); And (fuel/core/classes/fuel.php - line 37): const TEST = 'test'; sooo.. the environment needs to be lowcase or the bootstrap - line 30 should have a strtolower on it, right? A quick vhost test will reproduce this issue. Best regards,
    Fabio.
  • I made a blog post about this topic here: http://fbta.tumblr.com/post/15236734096/fuelphp-error-invalid-data-source-name Good to know you sorted it out though.... :)
  • Good post. You should probably add the 3rd option of setting up the server environment incorrectly (or not at all) through the Apache config files. That's what happened to me.
  • Environment names are lowercase, yes. This has already been corrected in the current dev docs.
  • otoso wrote on Wednesday 28th of December 2011:
    OMG. After a bit more digging around I found out what the problem was. I was setting the server environment variable incorrectly so the appropriate config was not being read. It's not entirely clear from the docs how to actually set the server environment variables. After a bit of testing here is how it should be set for Apache web server: SetEnv FUEL_ENV DEVELOPMENT Hope this helps someone.

    THANKS!! Can someone update the docs !!! LOL

Howdy, Stranger!

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

In this Discussion