php pdo wrapper class examples

There are many pdo wrapper class out there, but I have decided to download and use php pdo wrapper class because I am already using the form builder class created by the same developer so it makes it easier for me to follow because of the same design pattern. Anyway, I am writing this post to document what I learned about the class by providing some examples in hopes to help myself as a reference in the future as well as helping others with the same questions.

These examples are given with the assumption that you have been somewhat exposed to the class and have basic understanding of mysql queries.

Example # 1.

$existqry = mysql_query("select * from table where userid='".$_SESSION['userid']."'");
if(mysql_num_rows($existpqry)>0){
     return true;
}else{
     return false;
}

Example # 1 using php pdo wrapper class.

$existqry = $db->query('SELECT * FROM table where userid="'.$_SESSION['userid'].'"')->fetchColumn();
if(!empty($existqry)){
     return true;
}else{
     return false;
}

Example # 2.

$qry = mysql_query("select * from table where userid='".$_SESSION['userid']."'");
$res = mysql_fetch_assoc($qry);
$page    = $res['page'];
$page_id = $res['page_id'];
print "My ".$page." id is ".$page_id.".";

Example # 2 using php pdo wrapper class.

$bind = array(
  ":userid" => $_SESSION['userid'],
);
$qry = $db->select("table", "userid=:userid", $bind);
foreach ($qry as $res)
{
     $page    = $res['page'];
     $page_id = $res['page_id'];
}
print "My ".$page." id is ".$page_id.".";

 

Example # 3.

$qry = mysql_query("select * from table where userid='".$_SESSION['userid']."'");
while($res = mysql_fetch_assoc($qry)){
     //loop results
     echo $res['page_id'].'<br>';
}

Example # 3 using php pdo wrapper class.

$bind = array( ":userid" => $_SESSION['userid'], ); 
$qry = $db->select("table", "userid=:userid", $bind); 
foreach ($qry as $res) { 
     //loop results
     echo $res['page_id'].'<br>';
}

More to come. If you have an example you want to share, or if you think you can improve my examples, please post using comment below and I will add them here if they are applicable.

Leave a Reply