MySQL查询的结果不存储在数组中

时间:2014-02-22 12:00:12

标签: php mysql opencart

我试图将MySQL查询的结果存储到数组中,然后我想在打开的购物车中将数组元素显示在前端。代码如下:

public function getProducttoquatation($quatation_id)
{
    $query = $this->db->query("SELECT product_id AS product_id FROM ". DB_PREFIX. "quatation_products WHERE  quatation_id = '". $quatation_id. "'");

    $product_id=array();

   if($query->numrows)
   {
      foreach($query->rows as $row)
      {
        $product_id[] = array(
                            'product_id' => $row['product_id'],
                            'id' => $row['id']
                             );
      }
      return $product_id;
   }
   else
   {
      return false;
   }
}

在这里我发现了如下错误:

  

未定义的属性:stdClass :: $ numrows

如何存储此问题 在这里,如果我尝试写:

$product_id=array("58","69");

然后返回此数组,然后它正常工作。 但我希望数组中的这些值应该是来自MySQL查询的动态。 怎么解决这个?

2 个答案:

答案 0 :(得分:0)

试试这个

if(($query->num_rows)>0){

而不是

if($query->numrows){

更新2:

public function getProducttoquatation($quatation_id){


            $query = $this->db->query("SELECT product_id AS product_id FROM " . DB_PREFIX . "quatation_products WHERE  quatation_id = '" . $quatation_id . "'");

$product_id=array();

            if(($query->num_rows)>0){
                foreach($query->rows as $row){
                    $product_id[] = array(
                        'product_id' => $row['product_id'],
                    );
                }
                return $product_id;
            }
            else{
                return false;
            }
    }

答案 1 :(得分:-1)

试试这个

public function getProducttoquatation($quatation_id)
{
    $query = $this->db->query("SELECT product_id AS product_id FROM " . DB_PREFIX . "quatation_products WHERE  quatation_id = '" . $quatation_id . "'");

    $product_id=array();

   if($query)
   {
      foreach($query->rows as $row)
      {
        $product_id[] = array(
                            'product_id' => $row['product_id'],
                            'id' => $row['id']
                             );
      }
      return $product_id;
   }
   else
   {
      return false;
   }
}