Question

I had a look at a number of related answers for this question but couldn't find one that applies to this case.

$DatabaseConfig = array('DB_TYPE' => 'MySQL',
                        'DB_HOST' => 'localhost',
                        'DB_NAME' => 'simpleCMS',
                        'DB_USER' => 'root',
                        'DB_PASS' => 'mypass');

$dsn = $this->DatabaseConfig['DB_TYPE'].':host='.$this->DatabaseConfig['DB_HOST'].';dbname='.$this->DatabaseConfig;

I'm getting a Notice: Array to string conversion and the there's no connection.
Everything works fine when I insert the parameters manually.

How would I convert the key values into strings in this case? I tried placing (string) in various places but no luck.
Also, slightly unrelated, but if someone has any thoughts on making this $dsn a little less verbose? I couldn't find a cleaner design.

EDIT
Thank you everyone pointing out I was just missing the last parameter. The Notice: Array to string conversion is no longer visible.
I am however, presented with a new fatal error:
Fatal error: Call to undefined method PDOException::query()
And PDOException:
exception 'PDOException' with message 'could not find driver'

But again, when I enter the parameters manually, the connection works fine.
Why is the $dsn in it's current format not accepted?

Was it helpful?

Solution

PDO driver names are apparently case sensitive. Use mysql instead of MySQL

$DatabaseConfig = array('DB_TYPE' => 'mysql',
                        'DB_HOST' => 'localhost',
                        'DB_NAME' => 'simpleCMS',
                        'DB_USER' => 'root',
                        'DB_PASS' => 'mypass');

As additional cause for concern, this error message implies you have tried to call query() on an object which is actually a PDOException. Your try/catch logic wherein you establish the PDO object warrants additional attention, though you have not posted it. Verify that the variable you're storing the PDO object in doesn't somewhere get reused by the exception handling...

Fatal error: Call to undefined method PDOException::query()

OTHER TIPS

Based on what you provided, below is how to access the array. Not sure why you are using object notation?

$DatabaseConfig = array('DB_TYPE' => 'MySQL', 'DB_HOST' => 'localhost', 'DB_NAME' => 'simpleCMS', 'DB_USER' => 'root', 'DB_PASS' => 'mypass');

$dsn = $DatabaseConfig['DB_TYPE'].':host='.$DatabaseConfig['DB_HOST'].';dbname='.$DatabaseConfig**['DB_NAME']**;

(you were missing the DB_NAME key)

But, you also had an issue in your original script with ';dbname='.$DatabaseConfig;

Notice you didn't specify an array key to access. You only specified $DatabaseConfig. This is where the error "Array to string" occurs.

Your final setting seems to be missing ($this->DatabaseConfig['DB_NAME']):

$DatabaseConfig = array('DB_TYPE' => 'MySQL',
                    'DB_HOST' => 'localhost',
                    'DB_NAME' => 'simpleCMS',
                    'DB_USER' => 'root',
                    'DB_PASS' => 'mypass');

$dsn = $this->DatabaseConfig['DB_TYPE'].':host='.$this->DatabaseConfig['DB_HOST'].';
dbname='.$this->DatabaseConfig['DB_NAME'];
$dsn = $this->{DatabaseConfig}['DB_TYPE'].':host='.$this->{DatabaseConfig}['DB_HOST'].';dbname='.$this->{DatabaseConfig};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top