Created
May 31, 2013 14:27
-
-
Save jlittlejohn/5685346 to your computer and use it in GitHub Desktop.
PHP: PHP Data Object Class
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 | |
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