RE: [PHP] How to make id/name table into array?
| From: | John W. Holmes | Date: | Tue, 21 Jan 2003 09:01:16 +0000 |
| Subject: | RE: [PHP] How to make id/name table into array? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-132362@lists.php.net to get a copy of this message | ||
> Is there a better way to do this in PHP? It seems that a single number
> (key) and single name (value) would lend itself perfectly to an
(indexed
> or associative) array. I would think there is some PHP API call that
I'm
> unaware of that will just take the whole result set and stuff it into
an
> array for me no? If not, there should be. Perfect for things like
> ListBoxes, or table lookups so you don't have to hit the database
> constantly with joins et. al.
>
> $sql = "SELECT id, name FROM mytable ORDER BY id";
> if ( !($result = mysql_query($sql,$link)) ) { echo "failed\n"; }
> while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
> $myarray[$row['id']] = $row['name'];
That's the way to do it.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/