Skip to content

Instantly share code, notes, and snippets.

@SenpaiSilver
Created August 8, 2016 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiSilver/fd10cf6e90eba58d777c90a79d433baf to your computer and use it in GitHub Desktop.
Save SenpaiSilver/fd10cf6e90eba58d777c90a79d433baf to your computer and use it in GitHub Desktop.
<?php
/*
** Please define SQL_*
*/
class Sql
{
private $_SQL;
public function __construct($user = SQL_USR, $password =
SQL_PSSWD, $database = SQL_DB, $host = SQL_HOST)
{
try
{
$this->_SQL = new PDO('mysql:host='
.$host. ';dbname=' .$database, $user, $password);
}
catch(Exception $e)
{
echo($e->getMessage());
}
}
public function __destruct()
{
}
public static function PrefixQuery($str)
{
$words = array("INNER", "LEFT", "RIGHT", "JOIN",
"ON");
$i = -1;
$flag = 0;
$tmp = "";
if (SQL_PREFIXE == "")
return ($str);
$str = str_replace(',', ', ', $str);
$rray = explode(' ', $str);
$size = count($rray);
while (++$i < $size)
{
while ($rray[$i] == "")
++$i;
if ($rray[$i] == "WHERE")
$flag = 2;
if ($i != 0)
$tmp .= ' ';
if ($flag == 1 && !in_array($rray[$i],
$words))
$tmp .= SQL_PREFIXE.$rray[$i];
else
{
if
(preg_match('/^[A-Za-z0-9_-]+\.{1}[A-Za-z0-9_-]+$/', $rray[$i]))
$tmp .=
SQL_PREFIXE.$rray[$i];
else
$tmp .= $rray[$i];
}
if ($rray[$i] == "FROM")
$flag = 1;
}
return ($tmp);
}
public function Query($q)
{
$query = $this->_SQL->query($q);
return ($query);
}
public function Execute($query, $data = NULL)
{
$qry = $this->_SQL->prepare($query);
if ($data == NULL)
return ($qry->execute());
return ($qry->execute($data));
}
public function ExecuteAll($query, $data = NULL)
{
$qry = $this->_SQL->prepare($query);
if ($data != NULL)
{
if (!$qry->execute($data))
return (false);
}
else
$qry->execute();
$r = $qry->fetchAll();
return ($r);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment