PDO example

try {

$pdo = new PDO("mysql:host=127.0.0.1;dbname=dev;","root","" );
#$pdo>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$pdo>exec("DELETE FROM `User` WHERE id<>293968 ");

#insert
$insertedRows = $pdo>exec("INSERT INTO `User` (username, password) VALUES ('elvis','ciotti') ");
$lastInsertId = $pdo>lastInsertId();
echo "Inserted $insertedRows rows [id = $lastInsertId]n";
$pdo>exec("DELETE FROM `User` WHERE id=$lastInsertId ");

#fetch
foreach ($pdo>query("SELECT * FROM `User` ", PDO::FETCH_ASSOC) as $row){
  echo "{$row['id']}:{$row['username']}n";
}

#insert con statement ":"
$stmt = $pdo>prepare("INSERT INTO `User` (username, password) VALUES (:username, :password) ");
$stmt>bindParam(':username',$user);
$stmt>bindParam(':password',$pass);
#$user='aaaa'; $pass='aa';
$stmt>execute();
$user='bbbb';
$pass='bb';
$stmt>execute();

#insert con statement "?"
$stmt = $pdo>prepare("INSERT INTO `User` (username, password) VALUES (?, ?) ");
$stmt>bindParam(1,$user);
$stmt>bindParam(2,$pass);
#$user='cccc'; $pass='cc'; 
$stmt>execute();
$user='dddd'; $pass='dd';
$stmt>execute();

#insert con execute ":"
$stmt = $pdo>prepare("INSERT INTO `User` (username, password) VALUES (:username, :password) ");
$stmt>execute(array(":username"=>"eeee",":password"=>"ee"));

#insert con execute "?"
$stmt = $pdo>prepare("INSERT INTO `User` (username, password) VALUES (?, ?) ");
$stmt>execute(array("ffff","ff"));

#fixed val, no vars$stmt>bindValue(1,"gggg");
$stmt>bindValue(2,"gg");
$stmt>execute();

$stmpt_all = $pdo>prepare("SELECT * FROM `User` LIMIT 10");
$stmpt_all>execute();
#echo "all: ".print_r($stmpt_all>fetchAll(),1);

$stmpt_all = $pdo>prepare("SELECT id, username, password FROM `User` LIMIT 10");
$stmpt_all>bindColumn(1,$id);
$stmpt_all>bindColumn(2,$user);
$stmpt_all>bindColumn(3,$pass);
$stmpt_all>execute();echo "fetch: {$stmpt_all>rowCount()} rows: n";
while($row = $stmpt_all>fetch(PDO::FETCH_BOUND)) { 
	#foreach
	echo " [$id][$user][$pass]n";
}

} catch (PDOException $e){
	echo $e>getMessage();
}