简体   繁体   English

MySQL查询返回对象而不是数组

[英]MySQL Query returns the Object rather than Array

I'm trying to get the Array so i can loop it through and get the category_id from it. 我正在尝试获取数组,以便我可以遍历它并从中获取category_id。 Its output is in Odd behavior, 1st it comes in Public Row, then Public Rows, and then Num Rows. 它的输出是奇数行为,第一是公共行,然后是公共行,然后是数字行。 I'm not able to loop to these object as the loop code terminated in the 1st Row only and not going further in Rows. 我无法循环到这些对象,因为循环代码仅在第一行终止,而在行中不再继续。 Basically, the output query returns like this: 基本上,输出查询返回如下:

array (size=3)
  'row' => 
    array (size=2)
      'product_id' => string '50' (length=2)
      'category_id' => string '20' (length=2)
  'rows' => 
    array (size=2)
      0 => 
        array (size=2)
          'product_id' => string '50' (length=2)
          'category_id' => string '20' (length=2)
      1 => 
        array (size=2)
          'product_id' => string '50' (length=2)
          'category_id' => string '28' (length=2)
  'num_rows' => int 2

I'm trying to get the simple array so i can loop it dynamically and get the desired output. 我正在尝试获取简单的数组,以便可以动态循环它并获取所需的输出。 Here's my PHP code: 这是我的PHP代码:

$categories = (array)$this->db->query("SELECT * FROM " . DB_PREFIX ."product_to_category WHERE product_id=50");
var_dump($categories);  

However I've converted the Object into Array by prefixing (array) to the query but it doesn't worked for me as its still in object. 但是,我通过在查询前添加前缀(数组)将对象转换为数组,但由于它仍在对象中,因此对我不起作用。 Please suggest me as its taking too much time to settle this problem???? 请建议我,因为它花了太多时间来解决这个问题?

I DON'T KNOW WHY THIS IS COMING IN OBJECT?? 我不知道为什么会这样吗? BUT I GOT THE SOLUTION'S USING var_dump($categories['rows']); 但是我使用var_dump($ categories ['rows']);解决方案

If anybody know the exact solution your help might be acceptable. 如果有人知道确切的解决方案,您的帮助可能是可以接受的。

Which framework you are using ?? 您正在使用哪个框架? if you arr using Simple PHP look at this function mysql_fetch_array 如果您使用简单PHP来查看此函数mysql_fetch_array

return all the data in an array using function mysql_fetch_array 使用函数mysql_fetch_array返回数组中的所有数据

$categories = $this->db->query("SELECT * FROM " . DB_PREFIX ."product_to_category WHERE product_id=50");
var_dump(mysql_fetch_array($categories)); 

Font: http://us2.php.net/mysql_fetch_array 字体: http//us2.php.net/mysql_fetch_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM