1

I'm generally confused about the proper way to get data from a query once you execute. My end goal is to be able to show a list of all the records, but I'm not sure how to do it. I've tried a couple of different things but none have given me quite what I'm looking for. For example, when I search for Batman (which should have a record for [Batman, 500, 1993], and a record for [Batman, 499, 1993]) here's what I've tried:

$search->execute();

$result = $search->fetch(PDO::FETCH_ASSOC);
print_r($result);

Which gives me: Array ( [comicTitle] => Batman [comicIssue] => 500 [releaseDate] => 1993 )

That's more or less the format I'm looking for except that it's only one record instead of all of them.

$search->execute();

$result = $search->fetchAll(PDO::FETCH_ASSOC);
print_r($result);

Which gives me: Array ( [0] => Array ( [comicTitle] => Batman [comicIssue] => 500 [releaseDate] => 1993 ) [1] => Array ( [comicTitle] => Batman [comicIssue] => 499 [releaseDate] => 1993 ) )

$search->execute();

$result = $search->fetch(PDO::FETCH_ASSOC);
foreach($result as $row) {
        echo $row['comicTitle']."<BR>";
        echo $row['comicIssue']."<BR>";
        echo $row['releaseDate']."<BR>";

This echos 3 B's, 3 5's, and 3 1's with an illegal string offset warning between each.

So if I wanted to echo a line reading

"Title: " . $whatevertitle . " Issue: " . $whateverissue . " Release: " $whateverelease

for each record found, how do I go about actually getting those values from the returned array?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Midgetlegs
  • 35
  • 4

1 Answers1

1

using ->fetch(PDO::FETCH_ASSOC)

$search->execute();

while($row = $search->fetch(PDO::FETCH_ASSOC)){
        echo "Title: " . $row['comicTitle']."<BR>";
        echo "Issue: " . $row['comicIssue']."<BR>";
        echo "Release: " . $row['releaseDate']."<BR>";
}

using ->fetchAll(PDO::FETCH_ASSOC)

$search->execute();

$result = $search->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row) {
        echo "Title: " . $row['comicTitle']."<BR>";
        echo "Issue: " . $row['comicIssue']."<BR>";
        echo "Release: " . $row['releaseDate']."<BR>";
}
Sean
  • 12,443
  • 3
  • 29
  • 47