.A PHP/MySQL Gotcha, That’s Gotten Me for a Year
Ed note: If you’re not into coding PHP and MYSQL, you can just skip this one. Otherwise take heed.
If you make two consecutive calls in PHP to a MYSQL database using mysql_connect(), and both pass identical values for $server, $username, and $password, the second connection will not be established, it will silently use the existing one. It’s easy to force the second connection to establish by simply passing an optional boolean true as a fourth value to mysql_connect(), but dog have mercy on you if you don’t.
Twice last year I tried to move a sessions database from a dedicated server onto our newly built monster-sized primary database server, and twice I had to unmount it because I found database handlers were not pointing to the right database. Today we were testing a dev environment where mirrors of all our databases are mounted on a single server and we encountered the same problem again. But today Dog was merciful and sent us a blast of awareness as to why it was happening. And yea, with one quick trip tophp.net, it was there on the mysql_connect(), practically above fold, staring me right in the eye.
new_linkIf a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.
And there was great peace across the land. Dogmen!












Ted,
I love PHP and think that it is a very powerful language, yet I too have found a lot of similar quirks when developing, and unfortunately PHP debugging isn’t that friendly. While I develop primarily with Rails, I found that php.net documentation is much more helpful than Rails API, although ruby-forum.com is a good resource. Isn’t there a specific function to reload a connection?