php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38737 Invalid DSN doesn't raise exception, connects to server, doesn't select DB
Submitted: 2006-09-07 01:49 UTC Modified: 2006-09-08 22:15 UTC
From: mjsabby at gmail dot com Assigned: wez (profile)
Status: Not a bug Package: PDO related
PHP Version: 5.1.6 OS: Fedora Core 5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mjsabby at gmail dot com
New email:
PHP Version: OS:

 

 [2006-09-07 01:49 UTC] mjsabby at gmail dot com
Description:
------------
After looking closely at the previous bug, Bug #38736, which I filed, I realized I had an incorrectly formatted DSN string.

'mysql:localhost;dbname=xxx'

The above DSN, doesn't throw an exception. It instead goes aheads and connects to the MySQL server, but doesn't end up selecting the database.

I found this out (in reference to the previous bug) after I set the PDO mode to throw exceptions at everything to analyze what really was going wrong, to which it threw a SQL STATE 1046 No database selected ...

Is this desired functionality or a genuine bug?

Reproduce code:
---------------
<?php

define('DB_DSN', 'mysql:localhost;dbname=xxx');
define('DB_USER', 'xxx');
define('DB_PASS', 'xxx');

try {
 $dbh = new PDO(DB_DSN, DB_USER, DB_PASS);
} catch (PDOException $e) {
 die($e->getMessage());
}

// Didn't catch the exception

$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

try { $query = $dbh->query("QUERY Does not matter"); }
catch { PDOException $e) { echo $e->getMessage(); }

$dbh = null;

?>

Expected result:
----------------
An exception to be thrown, prompting an invalid DSN or something.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-07 01:54 UTC] mjsabby at gmail dot com
Ok, spoke to soon about the QUERY does not matter. I guess the query matters, so instead of that, put something valid :

"SELECT * FROM thisdoesnotmatter"
 [2006-09-08 20:49 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

PDO allows you to connect to a database without selecting any 
particular database, there is nothing wrong with that. So if 
dbname is not parsed due to faulty DSN, and subsequently not 
used to select db, there is no issue with the PDO code.
 [2006-09-08 20:59 UTC] mjsabby at gmail dot com
First off, an invalid DSN "should" throw an exception.

Secondly, there was nothing particularly wrong with the dbname =xxx syntax, the error was before the semicolon, which it happily went ahead and parsed, connected to localhost but did not select the database, which was correctly put.

So, I do think this warrants a fix, which is to throw an exception.
 [2006-09-08 22:15 UTC] iliaa@php.net
This DNS works, as your successful connection to the database 
indicates. If the host is not properly parsed the mysql PDO 
driver has a number of fallback mechanisms, which allows this 
to work. If connection could not be established, then yes, you 
would have an exception thrown.
 [2006-09-08 22:15 UTC] iliaa@php.net
see above.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 09:01:29 2024 UTC