0

I have created an array, as follows:

$results = array();
do {
  $results[] = $row_products;
} while ($row_products = mysql_fetch_assoc($products));

print_r($results);

This prints out the array like this:

Array ( 
[0] => Array ( 
       [productName] => product1 
       ) 
[1] => Array ( 
       [productName] => product2 
       ) 
[2] => Array ( 
       [productName] => product3 
     )

I want to now use say the second item in the array in another mysql query.

But I cannot define it. I have tried

$results[1];

but this does not work. So in effect, if I echo the second item, it would print 'product2'.

2 Answers 2

2

You should learn the basics about arrays here: http://www.php.net/manual/en/language.types.array.php

You are using a nested array, so you have the access it like this:

echo $results[1]['productName'];

Another solution would be to use $results[] = $row_products['productName']; and then just echo $results[1].

In addition, you should use a while loop instead of a do/while loop because $row_products does not seem to be defined for the first iteration.

while ($row_products = mysql_fetch_assoc($products)) {
  $results[] = $row_products;
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this :

echo $results[1]['productName'] ;

$results[1] is an array, If you want see the array print_r($results[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.