Re: [PHP] newbie PDO query display question

[email protected] (dealTek)
Newsgroups php.general
Message-ID <[email protected]>
On Jun 16, 2013, at 3:37 PM, Stephen <[email protected]> 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
[email protected]
[db-3]
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.