troubleshooting Question

How do you retrieve results from a PDO Array using this format?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
PHP
5 Comments2 Solutions154 ViewsLast Modified:
I've got to mimic the approach taken by some preexisting code in the way I retrieve the results from a SELECT statement. Generally, I use PDO::FETCH_OBJ just because it's familiar and straight forward. In this instance, I'm using PDO::FETCH_ASSOC and I'm stuck.

I've got a recordset of 32 rows. Here's my code thus far:

$selectTerms=$conn->prepare("SELECT * from terms order by discipline, term");
$selectTerms->execute();
$selectTermsCount=$selectTerms->rowCount();

$selectTermsArray=$selectTerms->fetch(PDO::FETCH_ASSOC);
for($x=0; $x<$selectTermsCount; $x++)
      {
      echo $selectTermsArray[$x]['term'].'<br>';
      }

Right now I'm getting an error that says I've got an undefined offset. If I do this:

echo $selectTermsArray['term']

I get the first row repeated 32 times.

How do I need to code this thing so I get my rows documented correctly?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros