Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm trying to get the Array so i can loop it through and get the category_id from it. 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:

$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']);

If anybody know the exact solution your help might be acceptable.

share|improve this question
    
Are you using any framework? like codeigniter etc? show me your definition of query function – Irfan DANISH Nov 18 '13 at 11:45

2 Answers 2

Which framework you are using ?? if you arr using Simple PHP look at this function mysql_fetch_array

share|improve this answer
    
I'm using Opencart, i've used mysql_fetch_array but it doesn't worked for me. – Sam Alam Nov 18 '13 at 11:47

return all the data in an array using function 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

share|improve this answer
    
ITS NOT WORKING, BASICALLY WHY THIS TYPE OF OUTPUT IS COMING. Basically i want to remove the 1st array, Num Rows from the above code i posted. It terminates after the 1st array, I've used several queries which return simple array like this: 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) – Sam Alam Nov 18 '13 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.