Skip to content

Instantly share code, notes, and snippets.

@shameerc
Created November 10, 2010 07:24
Show Gist options
  • Save shameerc/670494 to your computer and use it in GitHub Desktop.
Save shameerc/670494 to your computer and use it in GitHub Desktop.
PHP Pdo class
<?
$dsn = 'mysql:dbname=test;host=hostname';
$user = 'username';
$password = 'Password';
try {
$dbh = new PDO($dsn, $user, $password);
echo "Connected";
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$id = 1;
$sth = $dbh->prepare("SELECT * FROM testprepare where id = :id");
$sth->bindParam(':id',$id, PDO::PARAM_INT);
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
?>
<?php
$insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)");
$insert->execute(array('apple', 'green'));
$insert->execute(array('pear', 'yellow'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment