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();
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:
...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?
Thanks!