Created
September 11, 2012 04:13
-
-
Save ajcrites/3695911 to your computer and use it in GitHub Desktop.
Update from mysql_* to PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**#@+ | |
* 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