Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <?php
  2. /*
  3. ** Please define SQL_*
  4. */
  5.  
  6. class Sql
  7. {
  8. private $_SQL;
  9.  
  10. public function __construct($user = SQL_USR, $password =
  11. SQL_PSSWD, $database = SQL_DB, $host = SQL_HOST)
  12. {
  13. try
  14. {
  15. $this->_SQL = new PDO('mysql:host='
  16. .$host. ';dbname=' .$database, $user, $password);
  17. }
  18. catch(Exception $e)
  19. {
  20. echo($e->getMessage());
  21. }
  22. }
  23.  
  24. public function __destruct()
  25. {
  26. }
  27.  
  28. public static function PrefixQuery($str)
  29. {
  30. $words = array("INNER", "LEFT", "RIGHT", "JOIN",
  31. "ON");
  32. $i = -1;
  33. $flag = 0;
  34. $tmp = "";
  35.  
  36. if (SQL_PREFIXE == "")
  37. return ($str);
  38. $str = str_replace(',', ', ', $str);
  39. $rray = explode(' ', $str);
  40. $size = count($rray);
  41. while (++$i < $size)
  42. {
  43. while ($rray[$i] == "")
  44. ++$i;
  45. if ($rray[$i] == "WHERE")
  46. $flag = 2;
  47. if ($i != 0)
  48. $tmp .= ' ';
  49. if ($flag == 1 && !in_array($rray[$i],
  50. $words))
  51. $tmp .= SQL_PREFIXE.$rray[$i];
  52. else
  53. {
  54. if
  55. (preg_match('/^[A-Za-z0-9_-]+\.{1}[A-Za-z0-9_-]+$/', $rray[$i]))
  56. $tmp .=
  57. SQL_PREFIXE.$rray[$i];
  58. else
  59. $tmp .= $rray[$i];
  60. }
  61. if ($rray[$i] == "FROM")
  62. $flag = 1;
  63. }
  64. return ($tmp);
  65. }
  66.  
  67. public function Query($q)
  68. {
  69. $query = $this->_SQL->query($q);
  70. return ($query);
  71. }
  72.  
  73. public function Execute($query, $data = NULL)
  74. {
  75. $qry = $this->_SQL->prepare($query);
  76. if ($data == NULL)
  77. return ($qry->execute());
  78. return ($qry->execute($data));
  79. }
  80.  
  81. public function ExecuteAll($query, $data = NULL)
  82. {
  83. $qry = $this->_SQL->prepare($query);
  84. if ($data != NULL)
  85. {
  86. if (!$qry->execute($data))
  87. return (false);
  88. }
  89. else
  90. $qry->execute();
  91. $r = $qry->fetchAll();
  92. return ($r);
  93. }
  94. }
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement