j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Uncaught exception in PDO when using localhost #27

Closed philoye closed 13 years ago

philoye commented 13 years ago

It took me a while to figure out what was going on, but I am getting an exception when attempting to connect to the DB under localhost, that otherwise works fine for 127.0.0.1.

The culprit is PDO uses a socket connection when using 'localhost'. On my box for whatever reason mysql.sock is in a different place than the the PHP default. The fix is easy, simply add unix_socket=/path/to/mysql.sock, to the PDO connection string.

Obviously, your library doesn't have to work around a missing socket, but it probably shouldn't throw an exception showing the DB username/password. Perhaps in setup_db() wrap the connection in a try/catch block?

j4mie commented 13 years ago

Hi Phil

This is an interesting question. At the moment, Idiorm deliberately doesn't do anything with exceptions. It's designed to be a thin wrapper on top of SQL, and it lets the underlying library (PDO) raise any exceptions caused by the SQL it executes.

Really, your application should have a top-level exception handler to deal with things like this. I agree that showing the DB username and password in the backtrace isn't ideal, but isn't that a criticism of PDO rather than Idiorm?

Feel free to disagree :)

Jamie

philoye commented 13 years ago

I'm happy to deal with in my app. Its your library, you set the rules.

Incidentally, mysql.default_socket was actually correct, so I'm not sure why PDO was looking in the wrong place (/var/mysql/mysql.sock instead of /tmp/mysql.sock). The solution was easy, just pass along the unix_socket=".ini_get('mysql.default_socket') to the connection string when under localhost.

Thanks, p.