Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final protected function select($args = array(), $is_die = false){
- try {
- $this->sql = "SELECT ";
- if (isset($args['fields'])) {
- if (is_array($args['fields'])) {
- $this->sql .= implode(', ', $args['fields']);
- } else {
- $this->sql .= $args['fields'];
- }
- } else {
- $this->sql .= " * ";
- }
- $this->sql .= " FROM ";
- if (!isset($this->table) || empty($this->table)) {
- throw new Exception("Table not set");
- }
- $this->sql .= $this->table;
- /*Join Query*/
- if (isset($args['join']) && !empty($args['join'])) {
- $this->sql .= " ".$args['join'];
- }
- /*Join Query*/
- if (isset($args['where']) && !empty($args['where'])) {
- if (is_array($args['where'])) {
- $temp = array();
- foreach ($args['where'] as $column_name => $data) {
- if (!is_array($data)) {
- $data = array(
- 'value' => $data,
- 'operator' => '=',
- );
- }
- $str = $column_name.' '.$data['operator'].' :'.str_replace('.', '_', $column_name);
- $temp[] = $str;
- }
- $this->sql .= " WHERE ".implode(' AND ', $temp);
- } else {
- $this->sql .= " WHERE ".$args['where'];
- }
- }
- /*Group*/
- if (isset($args['group_by']) && !empty($args['group_by'])) {
- $this->sql .= " GROUP BY ".$args['group_by'];
- }
- /*Group*/
- /*Order*/
- if (isset($args['order_by']) && !empty($args['order_by'])) {
- $this->sql .= " ORDER BY ".$args['order_by'];
- } else {
- $this->sql .= " ORDER BY ".$this->table.".id DESC";
- }
- /*Order*/
- /*Limit*/
- if (isset($args['limit']) && !empty($args['limit'])) {
- if (is_array($args['limit'])) {
- $this->sql .= " LIMIT ".$args['limit'][0].",".$args['limit'][1];
- } else {
- $this->sql .= " LIMIT ".$args['limit'];
- }
- }
- /*Limit*/
- $this->stmt = $this->conn->prepare($this->sql);
- if (is_array($args['where']) || is_object($args['where'])){
- foreach ($args['where'] as $column_name => $data) {
- $value = is_array($data) ? $data['value'] : $data; //check if passed where statement was an array, fetch value if so
- if (is_int($value)) {
- $param = PDO::PARAM_INT;
- }elseif (is_bool($value)) {
- $param = PDO::PARAM_BOOL;
- }elseif (is_null($value)) {
- $param = PDO::PARAM_NULL;
- }else {
- $param = PDO::PARAM_STR;
- }
- if ($param) {
- $this->stmt->bindValue(":".str_replace('.', '_', $column_name), $value, $param);
- }
- }
- }
- if ($is_die) {
- echo $this->sql;
- debugger($this->stmt);
- debugger($args, true);
- }
- $this->stmt->execute();
- $data = $this->stmt->fetchAll(PDO::FETCH_OBJ);
- return $data;
- } catch (PDOException $e) {
- error_log(
- date('Y-m-d h:i:s A').", Select Query: ".$e->getMessage()."rn"
- , 3, ERROR_PATH.'/error.log');
- return false;
- } catch (Exception $e) {
- error_log(
- date('Y-m-d h:i:s A').", General: ".$e->getMessage()."rn"
- , 3, ERROR_PATH.'/error.log');
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement