2013-08-05 102 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; 

    } 

因此,我设法合并数组。