Accessing mysql_fetch_object with variable names
| From: | Baniz Daymov | Date: | Fri, 14 Nov 2008 12:49:10 +0000 |
| Subject: | Accessing mysql_fetch_object with variable names | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-283291@lists.php.net to get a copy of this message | ||
So, I have a newbie syntax question, sorry to bother you with noddy stuff.
I have fetched a set of results from a database quesry using
mysql_fetch_object. I want to iterate through a subset of the columns
from each result, as defined by a separate array. I don't seem to be
able to find the correct syntax for referencing the specific property
of the object with a variable name.
Here's my broken code:
$tabarray = array("bio", "research", "publications",
"supervision");
$staffdata = mysql_fetch_object( $result );
foreach ( $tabarray as $tab ) {
echo "<li>$staffdata->$tab</li>\n";
}
Hope that's clear what I'm trying to do. I know I could iterate
through every database column and discard any column not in tabarray
or use eval() to build a line of code that did the trick but I'm sure
there must be a more elegant way to get $staffdata->$tab to
interpolate to $staffdata->bio etc and I'm just too inexperienced to
know what it is.
Thanks in advance for your help folks.
--
BD