I have decided to go PDO and have started implementing it on one of my webprojects. I have limited programing skills but can usually pull it off with some tutorials and forums.
Here is my code this far and it works fine but how is the code correct regarding the picking up errors, syntax, order and begintransaction etc.? Have I missunderstood anything? Is anything unneccesary?
connect.php
<?php
try {
$DBH = new PDO("mysql:host=$host;dbname=$database", $username, $password, array(
PDO::ATTR_PERSISTENT => true
));
$DBH -> exec("set names utf8");
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
query.php
try {
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DBH->beginTransaction();
$STH = $DBH->prepare('SELECT id from users where uid = :uid');
$STH->setFetchMode(PDO::FETCH_ASSOC);
$STH->bindParam(':uid', $uid); // $uid value is set
$STH->execute();
$uid_in_db = $STH->rowCount();
if($uid_in_db==0){ //=new user, insert info in db.
$STH = $DBH->prepare("INSERT INTO USERS (uid,namn) VALUES (:uid, :name)");
$STH->bindParam(':uid', $uid);
$STH->bindParam(':namn', $_POST['namn']); // a value posted form user input
$STH->execute();
}
$DBH->commit(); //
} catch (Exception $e) {
$DBH->rollBack();
echo "Fel: " . $e->getMessage();
}
}else{
$error=1;
}
don't issue any implicit locks on the tables
? thanks! – Joseph Apr 18 '11 at 14:31