예제 #1
0
파일: sql.php 프로젝트: byronwall/runnDAILY
 /**
  * @param $as_object
  * @param $arr_on_single
  * @param $arr_index
  * @return mixed
  */
 function execute($as_object = false, $arr_on_single = false, $arr_index = null)
 {
     //set some defaults if needed
     if (!$this->_has_select) {
         $this->select("*");
     }
     if (!$this->_has_limit) {
         $this->limit(100);
     }
     $sql = $this->_get_full_sql();
     if (count($this->_bind_vals)) {
         $stmt = Database::getDB()->prepare($sql);
         $types = $this->_sql_all_bind_types();
         call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $this->_bind_vals));
         $stmt->execute() or RoutingEngine::throwException($stmt->error);
         $stmt->store_result();
     } else {
         $stmt = Database::getDB()->query($sql);
     }
     $results = array();
     while ($row = $stmt->fetch_assoc()) {
         if ($as_object) {
             $obj = new $this->_class($row);
             if (count($this->_left_joins)) {
                 foreach ($this->_left_joins as $table) {
                     $type = $this->_tables_classes[$table];
                     $obj->{$type} = new $type($row);
                 }
             }
             $results[] = $obj;
         } else {
             if (isset($arr_index)) {
                 $results[$row[$arr_index]] = $row;
             } else {
                 $results[] = $row;
             }
         }
     }
     $stmt->close();
     $return = !$arr_on_single && count($results) == 1 ? $results[0] : $results;
     if ($this->_has_debug) {
         var_dump($return);
     }
     return $return;
 }