Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO合并将结果合并到一个数组中_Php_Arrays_Pdo_Array Merge - Fatal编程技术网

Php PDO合并将结果合并到一个数组中

Php PDO合并将结果合并到一个数组中,php,arrays,pdo,array-merge,Php,Arrays,Pdo,Array Merge,我有一个查询结果。这是我的密码 $P = new Product(); echo "<pre>"; print_r($P->get_product_image_path(1)); echo "</pre>"; 这是我的数据库类函数 Array ( [0] => Array ( [picture] => 1328630682_1_xl.jpg ) [1] => Array

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

$P = new Product();
echo "<pre>";
print_r($P->get_product_image_path(1));
echo "</pre>";
这是我的数据库类函数

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
    )
但我想这样合并结果

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>";
我该怎么做呢。我是PDO新手,所以我不能做

  • 摆脱你的数据库类
  • 不要从数据库类扩展产品类
  • 使用PDO获取所需格式的数组
  • 来了

    public function queryColumn($index = NULL){
        $index = (isset($index)) ? intval($index) : 0;
        $this->execute();
        return $this->_sql->fetchAll(PDO::FETCH_COLUMN,$index);
    }
    
    公共函数get\u product\u image\u路径($productid)
    {
    $sql=“从“.PREFIX”中选择图片。“产品图片,其中产品id=:id”;
    $stm=$this->pdo->prepare($sql);
    $stm->execute(数组($productid));
    返回$stm->fetchAll(PDO::FETCH_列,0);
    }
    $pdo=新的pdo(…);
    $P=新产品($pdo);
    回声“;
    打印路径($P->获取产品图像路径(1));
    回声“;
    
    我编写了一个新的数据库类函数

    我编辑了我的函数

    因此,我设法合并阵列

    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>";
    
    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;
    
       }