Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Created May 31, 2013 14:27
Show Gist options
  • Save jlittlejohn/5685346 to your computer and use it in GitHub Desktop.
Save jlittlejohn/5685346 to your computer and use it in GitHub Desktop.
PHP: PHP Data Object Class
<?php
try{
/* Connect to Database */
$dsn = 'mysql:dbname=test;host=localhost';
$dbh = new PDO($dsn,'username','password');
/* Create SQL String with parameters */
$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
/* add values and execute request on database */
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
/* Get the returned Data */
$dataSet = $sth->fetchAll();
/* Print Results */
print_r($dataSet);
}catch (PDOException $e){
echo 'PDO Error: '.$e->getMessage();
}catch(Exception $e){
echo 'A Unknown Error Occurred: '.$e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment