php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41435 my.cnf database and host values are ignored in pdo_mysql
Submitted: 2007-05-18 14:50 UTC Modified: 2016-11-23 13:38 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: marques at displague dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.2.2 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-05-18 14:50 UTC] marques at displague dot com
Description:
------------
When creating a pdo_mysql connection, database and hostname are required even when they are supplied in the my.cnf file.

Reproduce code:
---------------
contents of ~/.my.cnf:
[php]
user = myusername
password = mypassword
database = mydb
host = db.mydomain.com
default-character-set = utf8

PHP code to test usage of ~/.my.cnf:
<?
$pdo = new PDO('mysql:dbname=ppv',null,null,array(PDO::MYSQL_ATTR_READ_DEFAULT_GROUP=> 'php');

$st = $pdo->prepare("select user()");
$st->execute();
$rs = $st->fetchAll();
print_r($rs);
?>





Expected result:
----------------
myusername@db.mydomain.com

Actual result:
--------------
Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name'

In the above example, "dbname" is still required even though a "database" was given in ~/.my.cnf.

The host given in ~/.my.cnf is also ignored, and PHP/mysql defaults to "localhost".

Interestingly enough, default_character_set is applied.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-18 14:52 UTC] marques at displague dot com
The example should have read: 
$ppv = PDO('mysql',null,null,array(PDO::MYSQL_ATTR_READ_DEFAULT_GROU
P=> 'php');
 [2007-05-18 15:10 UTC] marques at displague dot com
It looks like you can get either host or dbname to come from my.cnf, but not both, by leaving the value blank in the DSN as the final parameter.

example:
'mysql:dbname=' will use the database parameter in my.cnf
'mysql:host=' will use the host parameter in my.cnf
'mysql:dbname=:host='
SQLSTATE[42000] [1044] Access denied for user 'myusername'@'localhost' to database ':host='

'mysql:host=:dbname='
SQLSTATE[HY000] [2005] Unknown MySQL server host ':dbname=
 [2011-01-01 23:53 UTC] jani@php.net
-Package: Feature/Change Request +Package: MySQL related
 [2016-11-23 13:38 UTC] cmb@php.net
-Package: MySQL related +Package: PDO MySQL
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Thu Jun 21 10:01:55 2018 UTC