RE: Assign 2 values from Mysql to an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi this is similar to what Dollah Ihsan had suggested , however I ave
tweeked the getPopulationById function a bit.
/**
 * 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);
	While($row = mysql_fetch_assoc($result)) 
		$pop[$id][$row['country']][$row['name']] =
$row['population'];
	return $pop;
}


Chetan Dattaram Rane | Software Engineer | Persistent Systems
chetan_rane@xxxxxxxxxxxxxxxx  | Cell: +91 94033 66714 | Tel: +91 (0832) 30
79014
Innovation in software product design, development and delivery-
www.persistentsys.com




-----Original Message-----
From: Dollah Ihsan [mailto:dollah.ihsan@xxxxxxxxx] 
Sent: Friday, March 06, 2009 3:04 PM
To: Anton Heuschen
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  Assign 2 values from Mysql to an array

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@xxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux