Sunday, 15 September 2013

Efficient all purposes PDO query for all situation

Efficient all purposes PDO query for all situation

Rather than have different PHP functions for different interaction with
the database is it possible to have a one size fits all function. Below is
my attempt which works, but what are the limitations of this if any? And
will it unnecessarily lag the performance of the site?
function
fetch($field,$table,$where,$is,$limit,$offset,$order,$eqt,$function){
global $kdb;
if(!$eqt){ $eqt = "="; }
if(!$limit){ $limit = "1"; }
if(!$function){ $function = "SELECT"; }
$sql = "$function $field FROM $table";
$wheres = explode(',',$where); $is = explode(',',$is); $eqt =
explode(',',$eqt);
$eachcount = 0;
$eachby = "WHERE";
foreach($wheres as $where){
if(!$eqt[$eachcount]){
$eqt[$eachcount] = $eqt[0];
}
if($method == 'LIKE'){
$sql .= " $eachby $where $eqt[$eachcount] '%$is[$eachcount]%' ";
}
else {
$sql .= " $eachby $where $eqt[$eachcount] '$is[$eachcount]' ";
}
$eachby = "AND";
}
$stm = $kdb->prepare($sql);
$stm->execute();
if($function == 'count'){
$row = $stm->fetchColumn();
}
elseif($stm->rowCount() <= 1) {
$row = $stm->fetch();
}
else{
$row = $stm->fetchAll();
}
return $row;
}

No comments:

Post a Comment