Connecting
==============================
<?php
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
/*Other Codes*/
}catch(PDOException $e ){
echo "Error: ".$e;
}
?>
Excute query with secure data
==============================
<?php
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
$mysecuredata=14;
$db->query("Select * from table where id=".$mysecuredata);
}catch(PDOException $e ){
echo "Error: ".$e;
}
?>
Excute query with insecure data
==============================
<?php
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
$myinsecuredata=$_GET["id"];
$query=$db->prepare("Select * from table where id=?");
$query->excute(array($myinsecuredata));
}catch(PDOException $e ){
echo "Error: ".$e;
}
?>
Getting Data in database
==============================
<?php
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
$myinsecuredata=$_GET["table"];
$query=$db->prepare("Select * from ?");
$query->excute(array($myinsecuredata));
while($row=$query->fetch(PDO::FETCH_OBJ)) {
/*its getting data in line.And its an object*/
echo $row->yourcolumnname;
}
}catch(PDOException $e ){
echo "Error: ".$e;
}
?>
Reference
==============================
http://gencbilgin.net/pdo-kullanimi-php-de-veritabani-islemleri.html