Re: newbie PDO query display question
| From: | Ashley Sheridan | Date: | Mon, 17 Jun 2013 00:59:30 +0000 |
| Subject: | Re: newbie PDO query display question | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-321418@lists.php.net to get a copy of this message | ||
dealTek <dealtek@gmail.com> wrote:
>
>On Jun 16, 2013, at 3:37 PM, Stephen <stephen-d@rogers.com> wrote:
>
>> Here is a sample from my code:
>>
>> $photo_category_list = "";
>> $SQL = "SELECT category_id, category_name FROM gallery_category
>ORDER by category_order";
>> try {
>> $stmt = $dbh->prepare($SQL);
>> $stmt->execute();
>> while ( list( $id, $name) = $stmt->fetch(PDO::FETCH_NUM)) {
>> $photo_category_list .= "<option
>value=$id>$name</option>\n";
>> }
>> }
>> catch (PDOException $e){
>> echo 'Error getting category name. ' . $e->getMessage();
>> }
>> return $photo_category_list;
>>
>> Try to avoid your code assuming that you will just get one record
>back (unless you use Limit 1)
>>
>
>
>Thanks Stephen,
>
>Nice code!
>
>you mentioned ... - to avoid your code assuming that you will just get
>one record back
>
>But what about the case where you are deliberately just showing one
>record as in ---- FIND ONE PERSON like---- where ID = 101 ?
>
>do you still need to loop through when you just selected/displayed 1
>ITEM?
>
>Q: How do I display the columns without a loop in this 1 record
>case...?
>
>
>these fail - so what will work?
>
>echo $results["First"];
>echo $results["First"][0]; ???
>echo $results["First"][1]; ???
>
>
>
>> While works better than foreach for getting the result rows from PDO
>
>
>
>
>
>
>
>
>--
>Thanks,
>Dave - DealTek
>dealtek@gmail.com
>[db-3]
Have you tried chaining the the fetch() method on as in:
$pdo_object->query()->fetch();
Thanks,
Ash