Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created June 16, 2012 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeRogers0/2941987 to your computer and use it in GitHub Desktop.
Save MikeRogers0/2941987 to your computer and use it in GitHub Desktop.
PDO (PHP Data Objects) – Starter Guide
<?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:' => 'me@email.com'));
$result = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<?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();
}
?>
<?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