1

I know that $stmt->fetchAll(PDO::FETCH_ASSOC); returns the result in an array to the variable, but how would you go around extracting the information from the array? Say the variable holds 3000 entries.

Thank you

4
  • it gives you a two-dimensional array... either loop through the array or access a particular record via it's zero-based index. Commented Aug 18, 2013 at 2:41
  • @orangepill What did you mean by two dimensional? Assming the variable is $hello = $stmt->fetchAll(PDO::FETCH_ASSOC); I can't just do $hello[0][blah]? Commented Aug 18, 2013 at 2:45
  • I mean that it is an array of arrays. you would access the field name id in the first record by $hello[0]["id"]; Commented Aug 18, 2013 at 2:46
  • or each of the row's id elements in turn via foreach($hello as $row){ echo $row["id"]; } Commented Aug 18, 2013 at 2:47

1 Answer 1

1

If you mean extract information from what $stmt->fetchAll(PDO::FETCH_ASSOC); returns, you need to set it to a variable.

$returned_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($returned_results as $key=>$result) {
     echo "<pre>"; var_dump($result); echo "</pre>";
}

This will dump every result, which will show you how to directly access the array hierarchy.
Which will end up being something like:

$returned_results[2]['somefield'];
Sign up to request clarification or add additional context in comments.

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.