Re: PEAR::DB Question about array
| From: | Justin Patrin | Date: | Wed, 13 Apr 2005 18:21:29 +0000 |
| Subject: | Re: PEAR::DB Question about array | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18734@lists.php.net to get a copy of this message | ||
On 4/13/05, Oliver Schalch <oli@gentoo.ch> wrote:
> Hi,
>
> Okey I'm quite new to that one but I'll ask anyway, I'm trying to build
> a function which returns the results array with, of course, the content
> of the Query, I have tried:
>
> function getUserInfo($username) {
> $db =& DB::connect(DSN);
> if (PEAR::isError($db)) {
> die($db->getMessage());
> }
> $db->setFetchMode(DB_FETCHMODE_ASSOC);
> $res =& $db->query("BIG SELECT QUERY");
> if (PEAR::isError($res)) {
> die($res->getMessage());
> }
>
> return $res; // that should return the result array
> }
>
> I guess, that its not retur that array I really want in my main program.
> So, I'm a bit confused, I think I cannot return an array while in the
> FetchInto Loop.
>
> I'd like to call the elements of my array in the mainloop then with
> echo $res['element']; or something like that.
>
> Thanks for any hints!
>
You need to read more of the manual or search for PEAR::DB tutorials.
$recs = $db->getAll('SELECT * FROM table', DB_FETCHMODE_ASSOC);
However, this isn't a great way to do things unless you absolutely
*have* to have all of the data. It's better to do:
$sth = $db->query('SELECT * FROM table');
while ($rec = $sth->fetchRow(DB_FETCHMODE_ASSOC)) {
//process $rec
echo $rec['field'].'<br/>';
}
--
Justin Patrin