Re: Assign 2 values from Mysql to an array

From: Date: Fri, 06 Mar 2009 09:33:34 +0000
Subject: Re: Assign 2 values from Mysql to an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-289442@lists.php.net to get a copy of this message
I'm sorry if this is not what you're talking about... /** * assume your table structure just like this. * table: users * | id | country | name | population | * ------------------------------------------------------------------ * | 1 | Texas | Fort Worth | 400000 | * */ function getPopulationById($id) { // echo 'SELECT country, name, population FROM users WHERE id = ' . $id;die; $result = mysql_query('SELECT country, name, population FROM users WHERE id = ' . $id); if(mysql_num_rows($result) != 1) return false; $row = mysql_fetch_assoc($result); $pop[$id][$row['country']][$row['name']] = $row['population']; return $pop; } $array = getPopulationById(1); print_r($array); // print_r($array) should be like this: // Array ( [1] => Array ( [Texas] => Array ( [Fort Worth] => 400000 ) ) ) On Fri, Mar 6, 2009 at 3:41 PM, Anton Heuschen <antonfh@gmail.com> wrote: > > This might sound trivial, but for the live of me cant seem to get it to > work, and I am not familiar with such a thing. > > What I have is a query lets say : > > select  country,name,population from USERS where id= 'some_id' "; > > > Now I want to assign the result to one set (The above example might have 3+ > entries per telephone, thus what would be nice is something like an array of > : > > > [id][country][name]  = population .......... or to be exact if I echo the > value for each id and country and name to get the population value > > > Like : > > Array { > >  [id] array { >         [country] array { >                    [0] = USA >          } >  [name] array { >         [0] = test > >  } > > } > > > > > I dont know something like that, maybe Im over comlicating the question now > even, the main thing is wondered was in the first place was with a standard > query and variable assign, from the query like: > > select  country,name,population from USERS where id= 'some_id' "; > > normally you would just assign each field to one variable. like > > $country = result["country"] > $name = result["name"] > > > But now I want all 3 fields as one variable (which would be an array) ..but > how ? > > I hope I have not totally confused the question now. > > Thanks in advance

« previous php.general (#289442) next »