Skip to content

Instantly share code, notes, and snippets.

@kitelife
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save kitelife/9600017 to your computer and use it in GitHub Desktop.

Select an option

Save kitelife/9600017 to your computer and use it in GitHub Desktop.
PDO Usage sample
<?php
try {
$db_conn = new PDO('mysql:host=localhost;dbname=recipes', 'php-user', 'secret');
} catch (PDOException $e) {
echo "Could not connect to database";
exit;
}
$sql = 'SELECT name, description, chef
FROM receipes
WHERE id = :recipe_id';
try {
$stmt = $db_conn->prepare($sql);
if($stmt) {
// perform query
$result = $stmt->execute(array(
"recipe_id"=>1)
);
if($result) {
$recipe = $stmt->fetch();
print_r($recipe);
}else{
$error = $stmt->errorInfo();
echo "Query failed with message: " . $error[2];
}
}
} catch (PDOException $e) {
echo "A database problem has occurred: " . $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment