PHP userland backwards compatibility layer that emulates PHP 5.5+ core functions.

⌈⌋ ⎇ branch:  upgrade.php


Update of "pdo_mysql.php"

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: 9ef43b2fbfa896c6cb5184ebbb6f74f2493c1e10
Page Name:pdo_mysql.php
Date: 2014-03-18 02:34:00
Original User: mario
Mimetype:text/x-markdown
Next ca9b774511bd33e4cd063661dad46f9b5d0aac17
Content

pdo_mysql.php provides pdo_ lookalike wrapper functions to more easily replace their mysql_ counterparts.

Tutorial

See Q: Why shouldn't I use mysql_* functions in PHP? - A: Easy of Use / Ain't nobody got time for bindValue() for a nice summary.

Rewriting mysql_ code

Transitioning off the dated and cumbersome mysql_ functions can be easy.

  • Include pdo_mysql.php in your code.
  • And simply replace all mysql_ prefixes with pdo_

You can then also utilize parameterized queries with little effort:

Which usually leads to more readable code, and implicitly uses PDO behind the scenes.

Parameterized queries

Resulting in less fuzz behind the scenes:

pdo_mysql.php defaults to real prepared statements.

Advantages over plain PDO interface

The instantiation and query functions contain some built-in defaults:

  • Enable ERRMODE_WARNING per default.
  • Disable ATTR_EMULATE_PREPARES (some calls temporarily re-enable it).
  • MYSQL_ATTR_FOUND_ROWS is automatically applied for SELECT queries. Thus pdo_affected_rows() works on updates and retrieval queries.

Behind the scenes

When utilizing pdo_connect() and pdo_query() etc. you get an implicit global variable $pdo. This can be shared with more contemporary code, utilizing the actual OO PDO interface.

PDOStatement_Seekable is a result wrapper, which facilitates scrolling among results. Commonly you should just foreach over the result list. But code previously using mysql_ often seeks forth and back. A small buffer is implemented in PDOStatement_Seekable for this.