2014-09-30 43 views
0

我一直在試圖從數據庫表輸出數據,但我似乎無法做到,無論我做什麼,它都不會顯示出來。我嘗試使用foreach但沒有骰子。此代碼從phpacademy教程拍攝,我試圖通過添加一些功能,如能夠使用PDOPDO,輸出表中的所有數據

$bgItems = DB::getInstance()->getAll('background_image'); 

echo '<pre>', var_dump($bgItems), '</pre>'; 

foreach($bgItems as $bgItem) { 
    echo $bgItem; 
} 

這是DB類的樣子與收集數據庫中的所有數據進行修改方法我試圖實現:

public function query($sql, $params = array()) { 
    $this->_error = false; 
     if($this->_query = $this->_pdo->prepare($sql)) { 
      $x = 1; 
      if(count($params)) { 
       foreach($params as $param) { 
        $this->_query->bindValue($x, $param); 
        $x++; 
       } 
      } 

      if($this->_query->execute()) { 
       $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); 
       $this->_count = $this->_query->rowCount(); 
      } else { 
       $this->_error = true; 
      } 
     } 

     return $this; 
    } 

    public function action($action, $table, $where = array()) { 
     if(count($where) === 3) { 
      $operators = array('=', '>', '<', '>=', '<='); 

      $field  = $where[0]; 
      $operator = $where[1]; 
      $value  = $where[2]; 

      if(in_array($operator, $operators)) { 
       $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; 
       if(!$this->query($sql, array($value))->error()) { 
        return $this; 
       } 
      } 
     } 

     else { 
      $sql = "{$action} FROM {$table}"; 
      if(!$this->query($sql)->error()) { 
       return $this; 
      } 
     } 

     return false; 
    } 

    public function getAll($table) { 
     return $this->action('SELECT *', $table); 
    } 

這是輸出試圖查看使用的var_dump,這顯然看起來像一個複雜的數組中檢索數據時,我得到。

object(DB)#3 (5) { 
    ["_pdo":"DB":private]=> 
    object(PDO)#4 (0) { 
    } 
    ["_query":"DB":private]=> 
    object(PDOStatement)#8 (1) { 
    ["queryString"]=> 
    string(30) "SELECT * FROM background_image" 
    } 
    ["_error":"DB":private]=> 
    bool(false) 
    ["_results":"DB":private]=> 
    array(3) { 
    [0]=> 
    object(stdClass)#7 (2) { 
     ["id"]=> 
     string(1) "1" 
     ["name"]=> 
     string(28) "4ee269991861331957002e21.jpg" 
    } 
    [1]=> 
    object(stdClass)#9 (2) { 
     ["id"]=> 
     string(1) "2" 
     ["name"]=> 
     string(36) "22769-interesting-cat-meme-rv31.jpeg" 
    } 
    [2]=> 
    object(stdClass)#10 (2) { 
     ["id"]=> 
     string(1) "3" 
     ["name"]=> 
     string(50) "10645322_300758350130075_5393354656605964412_n.jpg" 
    } 
    } 
    ["_count":"DB":private]=> 
    int(3) 

}

修訂

這是結果時,直接返回從保護_results變量從DB類所採取的結果

array(3) { 
    [0]=> 
    object(stdClass)#7 (2) { 
    ["id"]=> 
    string(1) "1" 
    ["name"]=> 
    string(28) "4ee269991861331957002e21.jpg" 
    } 
    [1]=> 
    object(stdClass)#9 (2) { 
    ["id"]=> 
    string(1) "2" 
    ["name"]=> 
    string(36) "22769-interesting-cat-meme-rv31.jpeg" 
    } 
    [2]=> 
    object(stdClass)#10 (2) { 
    ["id"]=> 
    string(1) "3" 
    ["name"]=> 
    string(50) "10645322_300758350130075_5393354656605964412_n.jpg" 
    } 
} 
+0

我會建議你使用'notORM'庫轉換的對象到一個數組,以操縱它,它適用於PDO和很好的API – Saqueib 2014-09-30 10:45:38

回答

0

您需要返回結果不是一個類本身,你可以使用一個類,但你需要實現迭代器。

public function getAll($table) { 
     if($this->action('SELECT *', $table)) return $this->_results; 
     return false; 

} 
+0

我試着這樣做,但它仍然返回一個數組與stdClass。你能告訴我如何迭代這個來讓它出現嗎?我在主帖末尾加上了輸出。謝謝 – user3530303 2014-09-30 11:15:29

0

我只是用這個

class Convert { 
    public function objectToArray($d) 
    { 
     if (is_object($d)) { 
      // Gets the properties of the given object 
      // with get_object_vars function 
      $d = get_object_vars($d); 
     } 

     if (is_array($d)) { 
      /* 
      * Return array converted to object 
      * Using __FUNCTION__ (Magic constant) 
      * for recursive call 
      */ 
      return array_map([__CLASS__, __METHOD__], $d); 
     } else { 
      // Return array 
      return $d; 
     } 
    } 
}