Skip to content

Instantly share code, notes, and snippets.

@hanafiah
Created March 6, 2014 12:07
Show Gist options
  • Save hanafiah/9388251 to your computer and use it in GitHub Desktop.
Save hanafiah/9388251 to your computer and use it in GitHub Desktop.
<?php
$host = 'localhost';
$dbname = 'tutorial_pdo';
$user = 'root';
$pass = '';
try {
//$dbh , database handler aka db handler
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
//named place holder
$stmt = $dbh->prepare("INSERT INTO users ( username, full_name, email ) values ( :username, :full_name, :email )");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':full_name', $full_name);
$stmt->bindParam(':email', $email);
//insert 1st row
$username = 'ibnuyahya';
$full_name = 'Muhamad Hanafiah Yahya';
$email = '[email protected]';
$stmt->execute();
//insert 2nd row
$username = 'kamal';
$full_name = 'Ahmad Kamal';
$email = '[email protected]';
$stmt->execute();
//close db handler
$dbh = null;
} catch (PDOException $e) {
echo "ouch!... ada ralat.";
file_put_contents('error_log.txt', $e->getMessage() . PHP_EOL, FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment