Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. final protected function select($args = array(), $is_die = false){
  2. try {
  3.  
  4. $this->sql = "SELECT ";
  5. if (isset($args['fields'])) {
  6. if (is_array($args['fields'])) {
  7. $this->sql .= implode(', ', $args['fields']);
  8. } else {
  9. $this->sql .= $args['fields'];
  10. }
  11. } else {
  12. $this->sql .= " * ";
  13. }
  14. $this->sql .= " FROM ";
  15. if (!isset($this->table) || empty($this->table)) {
  16. throw new Exception("Table not set");
  17. }
  18. $this->sql .= $this->table;
  19.  
  20. /*Join Query*/
  21. if (isset($args['join']) && !empty($args['join'])) {
  22. $this->sql .= " ".$args['join'];
  23. }
  24. /*Join Query*/
  25.  
  26. if (isset($args['where']) && !empty($args['where'])) {
  27. if (is_array($args['where'])) {
  28. $temp = array();
  29. foreach ($args['where'] as $column_name => $data) {
  30. if (!is_array($data)) {
  31. $data = array(
  32. 'value' => $data,
  33. 'operator' => '=',
  34. );
  35. }
  36. $str = $column_name.' '.$data['operator'].' :'.str_replace('.', '_', $column_name);
  37. $temp[] = $str;
  38. }
  39. $this->sql .= " WHERE ".implode(' AND ', $temp);
  40. } else {
  41. $this->sql .= " WHERE ".$args['where'];
  42. }
  43. }
  44.  
  45. /*Group*/
  46. if (isset($args['group_by']) && !empty($args['group_by'])) {
  47. $this->sql .= " GROUP BY ".$args['group_by'];
  48. }
  49. /*Group*/
  50.  
  51. /*Order*/
  52. if (isset($args['order_by']) && !empty($args['order_by'])) {
  53. $this->sql .= " ORDER BY ".$args['order_by'];
  54. } else {
  55. $this->sql .= " ORDER BY ".$this->table.".id DESC";
  56. }
  57. /*Order*/
  58.  
  59. /*Limit*/
  60. if (isset($args['limit']) && !empty($args['limit'])) {
  61. if (is_array($args['limit'])) {
  62. $this->sql .= " LIMIT ".$args['limit'][0].",".$args['limit'][1];
  63. } else {
  64. $this->sql .= " LIMIT ".$args['limit'];
  65. }
  66. }
  67. /*Limit*/
  68. $this->stmt = $this->conn->prepare($this->sql);
  69. if (is_array($args['where']) || is_object($args['where'])){
  70.  
  71. foreach ($args['where'] as $column_name => $data) {
  72. $value = is_array($data) ? $data['value'] : $data; //check if passed where statement was an array, fetch value if so
  73. if (is_int($value)) {
  74. $param = PDO::PARAM_INT;
  75. }elseif (is_bool($value)) {
  76. $param = PDO::PARAM_BOOL;
  77. }elseif (is_null($value)) {
  78. $param = PDO::PARAM_NULL;
  79. }else {
  80. $param = PDO::PARAM_STR;
  81. }
  82. if ($param) {
  83. $this->stmt->bindValue(":".str_replace('.', '_', $column_name), $value, $param);
  84. }
  85. }
  86.  
  87. }
  88.  
  89. if ($is_die) {
  90. echo $this->sql;
  91. debugger($this->stmt);
  92. debugger($args, true);
  93. }
  94.  
  95. $this->stmt->execute();
  96. $data = $this->stmt->fetchAll(PDO::FETCH_OBJ);
  97. return $data;
  98. } catch (PDOException $e) {
  99.  
  100. error_log(
  101. date('Y-m-d h:i:s A').", Select Query: ".$e->getMessage()."rn"
  102. , 3, ERROR_PATH.'/error.log');
  103. return false;
  104. } catch (Exception $e) {
  105. error_log(
  106. date('Y-m-d h:i:s A').", General: ".$e->getMessage()."rn"
  107. , 3, ERROR_PATH.'/error.log');
  108. return false;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement