Web Hosting Talk







View Full Version : Array index - a field name instead of number


Lord Northern
03-26-2008, 01:35 AM
Hey.
I've got a question.
I use pear db through php to access mysql and it usually, as it's supposed to be, returns an array.
Now, as an index of a specific place of the array I use numbers.
For example if I do:

$tamdeedeedam = $db -> getRow("SELECT name,age,location FROM users WHERE id ='5'");
Now, if I want to access age I'd do:
$tamdeedeedam[1];
Is there any way to use the field name instead of number so I'd only have to do:
$tamdeedeedam['age'];
I think I saw it somewhere. Is it possible?

DoubleV
03-26-2008, 01:48 AM
Hi,
based on the documentation on the PEAR DB package I think you should use something like

$foo = $db->getRow("SELECT bar FROM dual WHERE john='doe'", DB_FETCHMODE_ASSOC);
var_dump($foo);

For more information: http://pear.php.net/package/DB/docs/1.7.14RC1/DB/DB_common.html#methodgetRow

Lord Northern
03-26-2008, 02:08 AM
Heh, I got it.
thanks!