PHP - 公共方法;无法获取mysql查询的返回值

时间:2015-10-13 16:11:28

标签: php oop pdo

首先,所有我都是OOP新手。我有一个函数调用我的product_photos表中的所有值。不知何故,我无法达到表列名称的返回值。它总是返回null。 我的方法:

public function getProductPhotosWebService() {
    $this->prepare('SELECT * FROM product_photos');
    $this->execute();
    $this->param = $this->fetchAll();
    return $this->param;
}

在我看来我使用方法;

$productPhotos  = $products->getProductPhotosWebService();

当我var_dump($productPhotos)结果时,它返回值;

array (size=388)
  0 => 
    object(stdClass)[9]
      public 'product_photo_id' => string '1' (length=1)
      public 'product_photo_name' => string 'B' (length=1)
      public 'product_id' => string '1' (length=1)
      public 'order_id' => string '1' (length=1)
      public 'add_date' => string '2015-05-11 21:59:22' (length=19)
      public 'add_user' => string '1' (length=1)
      public 'upd_date' => string '2015-10-13 16:03:32' (length=19)
      public 'upd_user' => string '1' (length=1)
      public 'visibility' => string '1' (length=1)
  1 => 
    object(stdClass)[10]
      public 'product_photo_id' => string '2' (length=1)
      public 'product_photo_name' => string 'B' (length=1)
      public 'product_id' => string '1' (length=1)
      public 'order_id' => string '2' (length=1)
      public 'add_date' => string '2015-05-11 21:59:35' (length=19)
      public 'add_user' => string '1' (length=1)
      public 'upd_date' => string '2015-05-11 21:59:35' (length=19)
      public 'upd_user' => string '1' (length=1)
      public 'visibility' => string '1' (length=1)
  2 => 
    object(stdClass)[11]
      public 'product_photo_id' => string '3' (length=1)
      public 'product_photo_name' => string 'B' (length=1)
      public 'product_id' => string '2' (length=1)
      public 'order_id' => string '3' (length=1)
      public 'add_date' => string '2015-05-13 12:29:50' (length=19)
      public 'add_user' => string '2' (length=1)
      public 'upd_date' => string '2015-10-13 16:03:34' (length=19)
      public 'upd_user' => string '1' (length=1)
      public 'visibility' => string '1' (length=1)

但是我无法使用$productPhotos->product_id它会返回NULL

我缺少什么? 任何帮助都很有用。

PS:我没有使用任何框架。

1 个答案:

答案 0 :(得分:1)

$productPhotos是一个数组。

foreach ($productPhotos as $productPhoto) {
    echo $productPhoto->product_id, "\n";
}