Re: [PHP] Assign 2 values from Mysql to an array
I will take theirs and modify it just a little more to create the print_r() output that the OP suggested. function getPopulationById($id) { $dataSet = array(); if ( ( $result = mysql_query('SELECT country, name, population FROM users WHERE id = ' . $id) !== false ) { while ( $row = mysql_fetch_assoc($result) ) { $dataSet[$id] = $row; } } return $dataSet; } This will return: Array { [id] array { [country] = USA [name] = test [population] = 123 } } OPs way would result in a layout like the following... Array { [id] array { [country] = array [0] = USA } [name] = array [0] = test } [population] = array [0] = 123 } } } I don't see a reason why the OP was suggesting that he do it this way... I can't see the reason for the extra nested array for the final country, name, and population values. Should be the value, nothing more. Chetan Rane wrote: > 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 |40| > * > */ > > 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_r...@persistent.co.in | 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.ih...@gmail.com] > Sent: Friday, March 06, 2009 3:04 PM > To: Anton Heuschen > Cc: php-general@lists.php.net > Subject: Re: [PHP] 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 |40| > * > */ > > 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] => 40 ) ) ) > > On Fri, Mar 6, 2009 at 3:41 PM, Anton Heuschen 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 >&
Re: [PHP] Assign 2 values from Mysql to an array
Anton Heuschen 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 not sure if this is what you want - but $sql="your query"; $result=mysql_query; while ($foo = mysql_fetch_array($result)) { $bar[]=$foo; } then - $bar[0]['country'] $bar[0]['name'] $bar[0]['population'] would represent one row of your query $bar[1]['country'] $bar[1]['name'] $bar[1]['population'] would represent another row of your query etc. Is that what you are looking for? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign 2 values from Mysql to an array
PJ schreef: > Shawn McKenzie wrote: >> Dollah Ihsan wrote: >> >>> 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 |40| >>> * >>> */ >>> >>> >> Woo hooo! Did we finally suceed from the union that's going to >> socialist shit? Are we once again the great Republic of Texas! >> >> > Oowooo watch that socialist shit... it may the only > thing that will save us from the ooze we're into bacause of the "old > ways" of W and the rest of the greedy and warmongering bastards of our > recent past. > But, then, we're going to fix all the shit with money from the taxpayers > to glorify the exploits of the very crooks who fucked us in the first > place... we just don't learn from the past, do we? Don't worry as we > trapse along, we'll just hang ourselves again and again... Dubya is a stinking socialist though ... no I don't grok that either. Hey Wait. let's kill this thread before someone gets hurt. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign 2 values from Mysql to an array
Anton Heuschen schreef: > 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. seems like you've done your best to make it sound as confusing as possible, this happens sometimes when you no longer see the wood for the trees :-) > > What I have is a query lets say : > > select country,name,population from USERS where id= 'some_id' "; > 'USERS' seems like an odd namew for the table in question. > > 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 per telephone? where do these telephones come from? is that a freudian slip? did the phone ring whilst you we're writing that sentence? keep reading ... it gets more php related as we go one ... > : > > > [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'm not grokking that array structure much. > > > > 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"] sidenote: I never see the point of assign the vars like that anyhow, why not just use $result['name'] and do away with the exytra $name var? > > > But now I want all 3 fields as one variable (which would be an array) ..but > how ? why? I'd guess it's because you want to do a 'look-up' of the data, that would suggest you don't want to be extracting the data on a row by row basis, but rather grab all the data and parse it into a format you can use for multiple look ups, here's a little class idea,maybe it offers some inspiration/insight/relief (it untested because I can't be bothered to setup a DB): abstract class Populations { static function getAll($type = null) { self::init(); if ($type) { return isset(self::$data[$type]) ? self::$data[$type] : null; } return self::$data; } static function byId($id) { return self::get('names', intval($id)); } static function byPlaceName($name) { return self::get('names', strtolower(trim($name))); } static function byCountryName($name) { return self::get('countries', strtolower(trim($name))); } static private function get($k, $v) { self::init(); if (isset(self::$data[$k][$v])) return self::$data[$k][$v]; return null; } private static function init() { if (!isset(self::$data)) self::loadData(); } private static function loadData() { self::$data = array( 'ids' => array(), 'names' => array(), 'countries' => array(), ); // let's assume that the following query will only return // a reasonable number of rows (i.e. not a very large number) // let's also assume that a db connection has been setup $res = mysql_query('SELECT id, country, name, population FROM users'); if ($res && mysql_num_rows($res)) { while ($row = mysql_fetch_assoc($res)) { $i = $row['id']; $n = strtolower(trim($row['name'])); $c = strtolower(trim($row['country'])); self::$data['ids'][$i] = self::$data['names'][$n] = $row['population']; if (!isset(self::$data['countries'][$c])) self::$data['countries'][$c] = 0; self::$data['countries'][$c] += $row['population']; } } } } you would use this class something like: echo Population::byId(1), "\n"; echo Population::byPlaceName("Texas"), "\n"; echo Population::byCountryName("USA"), "\n"; var_dump(Population::getAll("names")); var_dump(Population::getAll()); > > I hope I have not totally confused the question now. if you didn'
Re: [PHP] Assign 2 values from Mysql to an array
Shawn McKenzie wrote: > Dollah Ihsan wrote: > >> 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 |40| >> * >> */ >> >> > > Woo hooo! Did we finally suceed from the union that's going to > socialist shit? Are we once again the great Republic of Texas! > > Oowooo watch that socialist shit... it may the only thing that will save us from the ooze we're into bacause of the "old ways" of W and the rest of the greedy and warmongering bastards of our recent past. But, then, we're going to fix all the shit with money from the taxpayers to glorify the exploits of the very crooks who fucked us in the first place... we just don't learn from the past, do we? Don't worry as we trapse along, we'll just hang ourselves again and again... -- unheralded genius: "A clean desk is the sign of a dull mind. " - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign 2 values from Mysql to an array
Dollah Ihsan wrote: > 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 |40| > * > */ > Woo hooo! Did we finally suceed from the union that's going to socialist shit? Are we once again the great Republic of Texas! -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Assign 2 values from Mysql to an array
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 |40| * */ 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_r...@persistent.co.in | 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.ih...@gmail.com] Sent: Friday, March 06, 2009 3:04 PM To: Anton Heuschen Cc: php-general@lists.php.net Subject: Re: [PHP] 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 |40| * */ 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] => 40 ) ) ) On Fri, Mar 6, 2009 at 3:41 PM, Anton Heuschen 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
Re: [PHP] 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 |40| * */ 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] => 40 ) ) ) On Fri, Mar 6, 2009 at 3:41 PM, Anton Heuschen 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