Skip to content

Instantly share code, notes, and snippets.

@hanafiah
Last active August 29, 2015 13:57
Show Gist options
  • Save hanafiah/9406892 to your computer and use it in GitHub Desktop.
Save hanafiah/9406892 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);
$stmt = $dbh->prepare("UPDATE users SET full_name = :full_name WHERE id = :id ");
$stmt->bindParam(':full_name', $full_name, PDO::PARAM_STR);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
//Find this ID and update full name
$id = 3;
$full_name = 'kasim selamat';
$stmt->execute();
echo "Updated {$stmt->rowCount()} rows." ;
//check updated data
$stmt = $dbh->prepare("SELECT * FROM users WHERE id = ? ");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$id = 3;
$stmt->execute();
$row = $stmt->fetch();
echo '<pre>';
print_r($row);
echo '</pre>';
//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