After moving from mysql_ to PDO I created a PDO handling function. Basically this function takes parameters, checks them and returns end result. The thing is that I created this ~1 year ago and haven't really optimized it and after looking at it again I wanted to see if I can improve it somehow, but right now I can't really think of anything. So I came here.
Note: Code is long, but after writing hundreds of MySQL request it saves some space.
function UltimatePDO($Type, $DB, $SeWhere = "", $SePointer = "", $Values = "", $Param = "", $UpPointer = "", $Extra = "")
{
global $dbh;
global $abc;
if(empty($DB) || empty($Type))
{
PDOError("Not all parameters");
return false;
}
if($Type == "INSERT")
{
if(!empty($Values) && !empty($Param))
{
$Lenght = count($Values);
$Lenght1 = count($Param);
if($Lenght == $Lenght1)
{
$a = 1;
$Values[0] = Replace($Values[0]);
$Param[0] = Replace($Param[0]);
$Rez = array(":$Values[0]" => "$Param[0]");
$Rez1 = array();
$Rez1 = array_merge($Rez1, $Rez);
$New = ":$Values[0]";
$In = implode(",", $Values);
while($a < $Lenght)
{
$Values[$a] = Replace($Values[$a]);
$Param[$a] = Replace($Param[$a]);
$New .= ",:$Values[$a]";
$Rez = array(":$Values[$a]" => "$Param[$a]");
$Rez1 = array_merge($Rez1, $Rez);
$a++;
}
try
{
$stmt = $dbh->prepare("INSERT INTO $DB ($In) VALUES ($New)");
$stmt->execute($Rez1);
$Affected = $stmt->rowCount();
if($Affected)
{
return true;
}
else
{
PDOError("Can't insert into DB");
return false;
}
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
if($Type == "SELECT" || $Type == "COUNT")
{
if(!empty($SeWhere) && !empty($SePointer))
{
try
{
if($Type == "SELECT")
{
$stmt = $dbh->prepare("SELECT * FROM $DB WHERE $SeWhere = :user $Extra");
$stmt->execute(array(":user" => $SePointer));
return $stmt->fetch(PDO::FETCH_ASSOC);
}
if($Type == "COUNT")
{
$stmt = $dbh->prepare("SELECT COUNT(*) FROM $DB WHERE $SeWhere = :user");
$stmt->execute(array(":user" => $SePointer));
$count = $stmt->fetch(PDO::FETCH_NUM);
return $count[0];
}
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
if($Type == "SELECT NO PARAM" || $Type == "COUNT NO PARAM")
{
try
{
if($Type == "SELECT NO PARAM")
{
$stmt = $dbh->prepare("SELECT * FROM $DB $Extra");
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
if($Type == "COUNT NO PARAM")
{
$stmt = $dbh->prepare("SELECT COUNT(*) FROM $DB $Extra");
$stmt->execute();
$Re = $stmt->fetch(PDO::FETCH_NUM);
return $Re[0];
}
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
if($Type == "SELECT TABLE" || $Type == "SELECT TABLE NO PARAM")
{
if($Type == "SELECT TABLE")
{
if(!empty($SeWhere) && !empty($SePointer))
{
try
{
$stmt = $dbh->prepare("SELECT * FROM $DB WHERE $SeWhere = :user $Extra");
$stmt->execute(array(":user" => $SePointer));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
if($Type == "SELECT TABLE NO PARAM")
{
try
{
$stmt = $dbh->prepare("SELECT * FROM $DB $Extra");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
}
if($Type == "UPDATE")
{
if(!empty($Values) && !empty($Param) && !empty($UpPointer))
{
$Lenght = count($Values);
$Lenght1 = count($Param);
if($Lenght == $Lenght1)
{
$Set = array();
$Rez = array();
$Values[0] = Replace($Values[0]);
$Param[0] = Replace($Param[0]);
$First = array("$Values[0] = :$Values[0]");
$First1 = array(":$Values[0]" => "$Param[0]");
$Rez = array_merge($Rez, $First);
$Set = array_merge($Set, $First1);
$MakeID = array(":PDOUniqueId" => "$UpPointer[1]");
$a = 1;
while($a < $Lenght)
{
$Values[$a] = Replace($Values[$a]);
$Param[$a] = Replace($Param[$a]);
$First = array("$Values[$a] = :$Values[$a]");
$Rez = array_merge($Rez, $First);
$First1 = array(":$Values[$a]" => "$Param[$a]");
$Set = array_merge($Set, $First1);
$a++;
}
$Final = implode(",", $Rez);
$Bound = array_merge($Set, $MakeID);
$Id = "$UpPointer[0]";
try
{
$stmt = $dbh->prepare("UPDATE $DB SET $Final WHERE $Id = :PDOUniqueId");
$stmt->execute($Bound);
$Affected = $stmt->rowCount();
if($Affected)
{
return true;
}
else
{
PDOError("Can't update DB");
return false;
}
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
if($Type == "DELETE")
{
if(!empty($SeWhere) && !empty($SePointer))
{
try
{
$stmt = $dbh->prepare("DELETE FROM $DB WHERE $SeWhere = :user");
$stmt->execute(array(":user" => $SePointer));
$Affected = $stmt->rowCount();
if($Affected)
{
return true;
}
else
{
PDOError("Can't delete from DB");
}
}
catch (PDOException $e)
{
PDOError($e->getMessage());
return false;
}
}
else
{
PDOError("Not all parameters");
return false;
}
}
PDOError("Unknown type of request");
return false;
}
The function handles basic request such as update, insert, delete, select, count. In code it looks like this:
All values comes in array()
form, which seemed like easiest solution:
UltimatePDO("UPDATE", "DB", "", "", array("Column"), array("Value"), array("hash", $user));
As for function Replace()
, it just replaces unique symbols.