MySQL PDO extension

Have you been or are you in this situation:

  • Hmm.. PDO.. gotta do some wrapper.
  • My gosh, it's ugly, gotta do some wrapper.
  • Wtf, PDO. Can't learn. Gotta get some helper class.
  • [add here some angstic situation]

Yes? So, why on earth you want to wrap your already wrapped Christmas presents into another paper?!

If you don't know what are you doing, read next carefully: Do not wrap the wrapper.
You'll get in to the troubles in deep soulful developments.

If you can't learn PDO. You can. And you have to learn. It will help your life. Trust me.
If you think it's ugly. Look in to the mirror and repeat It is beautiful until you'll think so.

But, but, but if you know what are you doing and you need some life easifying routines. Please create an extension to PDO!

With an extension method you can use PDO as is, but also use functions created by you. Nothing gets broken nor your brains get damaged.

I did my own simple PDO extension: class.db.php, that you can use or get an inspiration for your own. I forked Imavex's class that didn't fit into my needs.

Call class like normally $sql = new db (); with default connection parameters.

Or you can call different database connection parameters like $sqlProd = new db ('production'); and $sqlDev = new db ('development');

And run prepared stuff normally

$stmt = $sql->prepare("SELECT * FROM table WHERE field = ?");
$stmt->execute(array("value"));
print_r($stmt->fetchAll(PDO::FETCH_ASSOC);

Or straightfully

$stmt = $sql->query("SELECT * FROM table WHERE field = 'value'");
print_r($stmt->fetchAll(PDO::FETCH_ASSOC); 

Or use your own functions

$stmt = $sql->select("table", "field = 'value'");
print_r($stmt);

$bind = array(':value'=>'text');
$stmt = $sql->select("table", "field = ':value'", $bind);
print_r($stmt);

Or

$stmt = $sql->run("SELECT * FROM table field = 'value'");
print_r($stmt);

There is also quite nice debugging function for all your needs. Find out more documentation on github.

0 comments

Your comment

Write the number three hundreds :