Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. class core_Query {
  2.  
  3. private $PDOInstance = null;
  4. private static $instance = null;
  5.  
  6. const DEFAULT_SQL_USER = DB_USER;
  7. const DEFAULT_SQL_HOST = CONFIGDB_SERVER;
  8. const DEFAULT_SQL_PASS = DB_PASSW;
  9. const DEFAULT_SQL_DTB = NAME_BASE;
  10.  
  11. function __construct() {
  12. $this->PDOInstance = new PDO('mysql:dbname=' . self::DEFAULT_SQL_DTB . ';charset=' . CHARSET_SQL . ';host=' . self::DEFAULT_SQL_HOST, self::DEFAULT_SQL_USER, self::DEFAULT_SQL_PASS);
  13. }
  14.  
  15. public static function getInstance() {
  16. if (is_null(self::$instance)) {
  17. self::$instance = new core_Query();
  18. }
  19. return self::$instance;
  20. }
  21.  
  22. public function query($query) {
  23. return $this->PDOInstance->query($query);
  24. }
  25.  
  26. public function exec($query) {
  27. return $this->PDOInstance->query($query);
  28. }
  29.  
  30. public function prepare($query) {
  31. return $this->PDOInstance->prepare($query);
  32. }
  33.  
  34. public function lastID($query) {
  35. return $this->PDOInstance->lastInsertId($query);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. public function read_Query($tb, $select, $where) {
  42. if (empty($select)) {
  43. $select = '*';
  44. }
  45. $result = core_Query::getInstance()->prepare('SELECT ' . $select . ' FROM ' . $tb . ' WHERE ' . $where . ' ');
  46. $result->execute();
  47. //print_r($result->errorInfo());
  48. return $result;
  49. }
  50.  
  51. public function insert_Query($tb, $data) {
  52. $insert = '';
  53. $bindinsert = '';
  54. $val_array = '';
  55. $i = 0;
  56. while(list($indice,$valeur) = each($data)){
  57.  
  58. $results = core_Query::getInstance()->query('SHOW COLUMNS FROM ' . $tb . ' ');
  59. while ($row = $results->fetch(PDO::FETCH_NUM)) {
  60. if($indice == $row[0]){
  61.  
  62. if ( $i > 0) {
  63. $insert .= ", ";
  64. $val_array .=", ";
  65. }
  66. $i++;
  67. $insert .= "$row[0]";
  68. $bindinsert[$row[0]] = $data[$row[0]];
  69. $val_array .=":$row[0]";
  70. }
  71. }
  72. }
  73.  
  74. $result = core_Query::getInstance()->prepare("INSERT INTO $tb ($insert) VALUES ($val_array)");
  75. while(list($indice,$valeur) = each($bindinsert)){
  76. $result->bindValue(':'.$indice.'', $valeur);
  77. }
  78. $result->execute();
  79. $id_tbKEY = core_Query::getInstance()->lastID();
  80. //print_r($result->errorInfo());
  81. return $id_tbKEY;
  82.  
  83. }
  84.  
  85. public function delete_Query($tb,$where) {
  86. $result = core_Query::getInstance()->prepare("DELETE FROM $tb WHERE ".$where." ");
  87. $result->execute();
  88. }
  89.  
  90. public function update_Query($tb, $where, $data) {
  91. $insert = '';
  92. $bindinsert = '';
  93. $i = 0;
  94. while(list($indice,$valeur) = each($data)){
  95. $results = core_Query::getInstance()->query('SHOW COLUMNS FROM ' . $tb . ' LIKE "'.$indice.'" ');
  96. while ($row = $results->fetch(PDO::FETCH_NUM)) {
  97. if($indice == $row[0]){
  98. if ( $i > 0) {
  99. $insert .= ", ";
  100. }
  101.  
  102. $insert .= "$indice = :$indice";
  103. $bindinsert[$row[0]] = $data[$row[0]];
  104. $i++;
  105. }
  106. }
  107. }
  108.  
  109. $result = core_Query::getInstance()->prepare("UPDATE $tb SET $insert WHERE $where ");
  110. while(list($indice,$valeur) = each($bindinsert)){
  111. $result->bindValue(':'.$indice.'', $valeur);
  112. }
  113. $result->execute();
  114. //print_r($result->errorInfo());
  115.  
  116. }
  117.  
  118. public function read_countQuery($tb,$select,$where) {
  119. if(!$select){ $select = '*'; }
  120. $result = core_Query::getInstance()->prepare("SELECT $select FROM $tb WHERE $where ");
  121. $result->execute();
  122. $num_rows = $result->rowCount();
  123. return $num_rows;
  124. }
  125.  
  126. public function RetourneNbRequetes(){
  127. return $this->NbRequetes;
  128. }
  129.  
  130. }
  131.  
  132.  
  133.  
  134. ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement