0

我有一个查询结果。这是我的代码。

$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 的新手,所以我做不到。

4

2 回答 2

-1

I wrote a new database class function

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

And i edited my function

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;

   }

Hence, i manage to merging arrays.

于 2013-08-05T17:38:02.190 回答
-1
  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>";
于 2013-08-05T16:42:27.300 回答