2013-08-05 58 views
0

我有一個查詢結果。這是我的代碼。PDO合併結果到一個數組

$P = new Product(); 
echo "<pre>"; 
print_r($P->get_product_image_path(1)); 
echo "</pre>"; 

在產品分類中,我有這個功能。

public function get_product_image_path($productid){ 
     $sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id"; 
     $this->query($sql); 
     $this->bind(':id', $productid); 
     if ($this->rowCount() > 0)     
      return $this->queryResults(); 
     else 
      return NULL; 

    } 

這裏是我的數據庫類的功能

public function query($sql) 
    { 
     return $this->_sql = $this->getPdo()->prepare($sql); 
    } 


    public function bind($param, $value, $type = null){ 
     if (is_null($type)) { 
      switch (true) { 
      case is_int($value): 
       $type = PDO::PARAM_INT; 
       break; 
      case is_bool($value): 
       $type = PDO::PARAM_BOOL; 
       break; 
      case is_null($value): 
       $type = PDO::PARAM_NULL; 
       break; 
      default: 
       $type = PDO::PARAM_STR; 
      } 
     } 
     $this->_sql->bindValue($param, $value, $type); 
    } 



    public function execute(){ 
     return $this->_sql->execute(); 
    } 



    public function queryResults(){ 
      $this->execute(); 
      return $this->_sql->fetchAll(PDO::FETCH_ASSOC); 
     } 

結果返回

Array 
(
    [0] => Array 
     (
      [picture] => 1328630682_1_xl.jpg 
     ) 

    [1] => Array 
     (
      [picture] => 1328630696_1_xl.jpg 
     ) 

    [2] => Array 
     (
      [picture] => 1328630689_1_xl.jpg 
     ) 

) 

但我想合併這樣的

Array 
    (
     [0] => 1328630682_1_xl.jpg 
     [1] => 1328630696_1_xl.jpg 
     [2] => 1328630689_1_xl.jpg 
    ) 

我該怎麼辦結果那。我是PDO的新手,這就是爲什麼我無法做到這一點。

回答

0
  1. 擺脫你的數據庫類。
  2. 不能從數據庫類擴展您的產品類。
  3. 使用PDO獲取數組所需形式

這裏有雲

public function get_product_image_path($productid) 
{ 
    $sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id"; 
    $stm = $this->pdo->prepare($sql); 
    $stm->execute(array($productid)); 
    return $stm->fetchAll(PDO::FETCH_COLUMN, 0); 
} 

$pdo = new PDO(...); 
$P = new Product($pdo); 
echo "<pre>"; 
print_r($P->get_product_image_path(1)); 
echo "</pre>"; 
-1

我寫了一個新的數據庫類功能

public function queryColumn($index = NULL){ 
    $index = (isset($index)) ? intval($index) : 0; 
    $this->execute(); 
    return $this->_sql->fetchAll(PDO::FETCH_COLUMN,$index); 
} 

而且我編輯功能

public function get_product_image_path($productid){ 
     $sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id"; 
     $this->query($sql); 
     $this->bind(':id', $productid); 
     if ($this->rowCount() > 0)     
      return $this->queryColumn(); 
     else 
      return NULL; 

    } 

因此,我設法合併數組。