Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created September 11, 2012 04:13
Show Gist options
  • Save ajcrites/3695911 to your computer and use it in GitHub Desktop.
Save ajcrites/3695911 to your computer and use it in GitHub Desktop.
Update from mysql_* to PDO
/**#@+
* OLD EVIL METHOD
*/
mysql_connect('localhost', $username, $password)
mysql_select_db('App');
$userid = mysql_real_escape_string($_GET['userid']);
$query = <<<SQL
SELECT
username
FROM
Users
WHERE
userid = '$userid'
SQL;
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo "Hello $row[username]";
/**#@-*/
/**#@+
* NEW GLORIOUS METHOD
*/
$db = new PDO('mysql:host=localhost;dbname=App', $username, $password)
$userid = $_GET['userid'];
$query = <<<SQL
SELECT
username
FROM
Users
WHERE
userid = ?
SQL;
$result = $db->prepare($query);
//execute does not return results; you must do processing on the object.
$result->execute($userid);
$row = $result->fetch();
echo "Hello $row[username]. Your data is safe with PDO!";
/**#@-*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment