Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

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

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?
PHP

Avatar of undefined
Last Comment
Member_2_248744

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bruce Gust

ASKER
Ray, I got it! Thanks for the article. I wound up going with fetchAll(PDO::FETCH_ASSOC); Not because I'm that sharp, but because I "just tried it" and it worked. After researching it, I saw it both in your article as well as the PHP Manual. So, there it is: The Captain Crunch Magic Decoder Ring. fetch(PDO::FETCH_ASSOC) will give you one row whereas fetchAll(PDO::FETCH_ASSOC) gives you everything.

Thanks!
Bruce Gust

ASKER
And Slick, I just saw your post. Thanks for validating what I was able to discover only after some trial and error.

As far as why I need all the rows, I've been tasked with replacing some deprecated code and every situation where a recordset needed to be retrieved the approach was to identify the number of rows in the recordset and then use a for loop to print everything. I replaced that with a "while" and a fetch(PDO::FETCH_OBJ) and it's worked just fine.

In this instance though, there was a specified number of times the code needed to loop through the recordset and I couldn't tell just how necessary it was, but I decided to preserve their thought process and do it like this:

$selectSystolicQueryArray=$selectSystolicQuery->fetchAll(PDO::FETCH_ASSOC);
	for($x=0; $x<$num; $x++) {

	/*
	SYSTOLIC;
	*/
	$sysrangeorder = $selectSystolicQueryArray[$x]['rangeorder'];

Open in new window


...and there you have it. Preserve their paradigm while incorporating the PDO dynamic.

Thanks!
Member_2_248744

thanks 4 points, , just a suggestion, it always better to NOT get rows from the database, if you are NOT going to use them, so maybe instead of the -
     for($x=0; $x<$num; $x++) {

you add to the SELECT string a LIMIT using the variable  $num , so you don't get rows you would not need?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck