Wednesday, March 12, 2008

mySQL PHP PEAR::DB

So, I just fixed a behavior in PEAR::DB. In isManip, a regular expression is called on every query that goes through the PEAR::DB layer. That sucks btw, but the purpose of function is to tell the classes that inherit from DB that the query passed is a query that affects data.

The code is as follows


function isManip($query)
{
$manips = 'INSERT|UPDATE|DELETE|REPLACE|'
. 'CREATE|DROP|'
. 'LOAD DATA|SELECT .* INTO .* FROM|COPY|'
. 'ALTER|GRANT|REVOKE|'
. 'LOCK|UNLOCK';
if (preg_match('/^\s*"?(' . $manips . ')\s+/i', $query)) {
return true;
}
return false;
}



Then in mysql.php there is this



function affectedRows()
{
if ($this->_last_query_manip) {
return @mysql_affected_rows($this->connection);
} else {
return 0;
}
}



Which is not ideal IMHO. I changed mysql.php to just return the results of mysql_affected_rows which is better anyway IMHO.

What DB wrapper do you use? What do you think about PDO?

Personally I like to write my own, bare bone wrappers around mysql_* functions for php but to make things compatible I'm using PEAR::DB.

5 comments:

Andre Medeiros said...

I've actually switched from the native mysql function calls to PDO, and I couldn't be happier. Besides being object-oriented, you can configure it in a way that it throws exceptions and it's a hell of a lot faster.

If you'd like to discuss it further, my msn contact is afmedeiros [at] eda [dot] pt

Anonymous said...

I use my own barebones one as well. A few generic db_XXX() functions. Supports multiple datasources, defaults to utf-8, in use on a lot of projects now spread over personal, corporate and freelance projects. I might "open source" it at some point as part of a mini "framework" of sorts.

Anonymous said...

This DB::isManip() is just broken by design. MDB2 does not rely on it and DB has been deprecated in favor of MDB2 since quite some time now.

Chess.com said...

Do you do mysql consulting in the bay area?

Erik
www.Chess.com
650.796.4810

Chess.com said...

oops. i didn't think comments would auto-publish - can you remove my last one :) i don't want my phone number out there...