Re: [PHP] newbie PDO query display question

[email protected] (jomali)
Newsgroups php.general
Message-ID <CAKh53YuAq7LAgE0U+WPVY8JdDqxLgWBx9T7LGEFMnd4=S9ajdQ@mail.gmail.com>
On Sun, Jun 16, 2013 at 6:29 PM, dealTek <[email protected]> wrote:

> Hi all,
>
> newbie PDO question...
>
> I think (hard to tell - buried in a wrapper class) I am doing a select
> query using  - PDO::FETCH_ASSOC like...
>
> wrapper ------ return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
>
> ---
>
> so my query is like...
>
> $results = $db->select("mytable", "id = 201"); //just 1 exact record)
>
> then I can loop like......
> foreach ($results as $result) {
>    ....
> ?>
>
>   <tr>
>     <td><?php echo $result["First"]; ?></td>
>     <td><?php echo $result["Last"]; ?></td>
>     <td><?php echo $result["id"]; ?></td>
>
>   </tr>
>
> This all works fine.....
>
> ---> But since I only have 1 exact record - I don't need to LOOP anything
> - so...
>
> Q: How do I display the columns without a loop
>
>
> these fail - so what will work
>
> echo $results["First"];
> echo $results["First"][0]; ???
> echo $results["First"][1]; ???
>
>
> PDO::fetchAll returns its data as an array, even if only one record is
returned. In this case,
echo $results[0]["First"];
echo $results[0]["Last];
echo $results[0]["id"];

will do what you want.

<snip>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.