Created
March 7, 2014 07:05
-
-
Save hanafiah/9406684 to your computer and use it in GitHub Desktop.
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 | |
$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("SELECT * FROM users WHERE id = ? "); | |
$stmt->bindParam(1, $id, PDO::PARAM_INT); | |
$stmt->setFetchMode(PDO::FETCH_ASSOC); | |
//Find this ID | |
$id = 3; | |
$stmt->execute(); | |
$row = $stmt->fetch(); | |
echo '<pre>'; | |
print_r($row); | |
echo '</pre>'; | |
/* | |
* if more than one record, can use loop | |
* | |
if ($stmt->execute()) { | |
while ($row = $stmt->fetch()) { | |
print_r($row); | |
} | |
} | |
*/ | |
//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