Re: Array?
| From: | Andrew Rush | Date: | Fri, 06 Oct 2000 20:37:28 +0000 |
| Subject: | Re: Array? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18974@lists.php.net to get a copy of this message | ||
On Friday, October 6, 2000, at 04:00 PM, Matt McClanahan wrote:
> On Fri, 6 Oct 2000, Joao Prado Maia wrote:
>
> > On Fri, 6 Oct 2000, Wee Chua wrote:
> >
> > > Hi all,
> > > I am newbie to PHP. How could call each field out manully from this array?
> > > Thanks in advance for any helps!
> > >
> > > $result = @mysql_query("Select * from
> > > Project,ProjType,Company,Event,EventType",$db);
> > > for ($i=0;$i<mysql_num_fields($result);$i++) {
> > >
> > > $field = mysql_field_name($result,$i);
> > >
> >
> > <?php
> > // sweet code to dynamically get all values from all fields
> > // and create dynamic variables with their names
> > $sql = "SELECT * FROM Project";
> > $result = @mysql_query($sql, $db);
> > $rows = @mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
while(list($key, $value)=each($row))
{
$$key=$value;
}
do more stuff here
}
works nicely, much less code. works with any result set.
if you are coding for a specific table where you know the field names you can trim this code down
even further to:
while(list($field1, $field2, $field3 [etc])=mysql_fetch_array($result)) {
do some stuff here
}