Created
June 16, 2012 17:16
-
-
Save MikeRogers0/2941987 to your computer and use it in GitHub Desktop.
PDO (PHP Data Objects) – Starter Guide
This file contains hidden or 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
<?php | |
$query = $db->prepare('SELECT * FROM `users` WHERE `ID` = :ID: AND `email` = :email: ORDER BY ID DESC LIMIT 0,1;'); | |
$query->execute(array(':ID:' => '3', ':email:' => '[email protected]')); | |
$result = $query->fetchAll(PDO::FETCH_ASSOC); | |
?> |
This file contains hidden or 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
<?php | |
// Define the parameters | |
$host = 'localhost'; | |
$dbname = 'my_database'; | |
$user = 'mysql_username'; | |
$pass = 'mysql_password'; | |
try { | |
// Call the PDO class. | |
$db= new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass); | |
} catch(PDOException $e) { | |
// If something goes wrong, PDO throws an exception with a nice error message. | |
echo $e->getMessage(); | |
} | |
?> |
This file contains hidden or 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
<?php | |
$query = $db->query('SELECT * FROM `users` ORDER BY ID DESC;'); | |
$result = $query->fetchAll(PDO::FETCH_ASSOC); | |
// $result will now contain an object of all the rows in the table 'Users' | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment