[PHP] mysql_fetch_array to associative array
Hello, I use the following GetArray for returning an array from mysql results. But having a hard time modifying it for returning a simple associative array Like: $conn-GetAssoc('SELECT id, name from manufacturers') Array ( [2] = BMW [1] = MAZDA [9] = FORD ) function GetArray($query) { $get = $this-Execute ( $query ); $row_id = 0; while ( $row = mysql_fetch_array($get) ) { foreach ( $row as $key = $value ) $result[$row_id][$key] = $value; $row_id++; } mysql_free_result($get); return $result; } function GetAssoc($query) { $get = $this-Execute ( $query ); $row_id = 0; while ( $row = mysql_fetch_array($get) ) { ? } mysql_free_result($get); return $result; } I searched a lot but didn't find anything. Thanks for any help :) Andras -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_fetch_array to associative array
On Mon, 2007-07-16 at 07:41 -0700, Andras Kende wrote: Hello, I use the following GetArray for returning an array from mysql results. But having a hard time modifying it for returning a simple associative array Like: $conn-GetAssoc('SELECT id, name from manufacturers') Array ( [2] = BMW [1] = MAZDA [9] = FORD ) function GetArray($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } function GetAssoc($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } I searched a lot but didn't find anything. Thanks for any help :) You're welcome. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_fetch_array to associative array
On Mon, 2007-07-16 at 10:52 -0400, Robert Cummings wrote: On Mon, 2007-07-16 at 07:41 -0700, Andras Kende wrote: Hello, I use the following GetArray for returning an array from mysql results. But having a hard time modifying it for returning a simple associative array Like: $conn-GetAssoc('SELECT id, name from manufacturers') Array ( [2] = BMW [1] = MAZDA [9] = FORD ) function GetArray($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } function GetAssoc($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { You should probably use mysql_fetch_assoc() in the above line. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_fetch_array to associative array
Andras Kende wrote: Hello, I use the following GetArray for returning an array from mysql results. But having a hard time modifying it for returning a simple associative array Like: $conn-GetAssoc('SELECT id, name from manufacturers') Array ( [2] = BMW [1] = MAZDA [9] = FORD ) function GetArray($query) { $get = $this-Execute ( $query ); don't you need to check to make sure that $get is a valid resource??? $result = array(); while ( $row = mysql_fetch_array($get) ) { All of this can be replace with this one line $result[] = $row; } mysql_free_result($get); return $result; } function GetAssoc($query) { $get = $this-Execute ( $query ); don't you need to check to make sure that $get is a valid resource??? $result = array(); while ( $row = mysql_fetch_assoc($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } I searched a lot but didn't find anything. Thanks for any help :) Andras -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql_fetch_array to associative array
-Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Monday, July 16, 2007 7:52 AM To: Andras Kende Cc: php-general@lists.php.net Subject: Re: [PHP] mysql_fetch_array to associative array On Mon, 2007-07-16 at 07:41 -0700, Andras Kende wrote: Hello, I use the following GetArray for returning an array from mysql results. But having a hard time modifying it for returning a simple associative array Like: $conn-GetAssoc('SELECT id, name from manufacturers') Array ( [2] = BMW [1] = MAZDA [9] = FORD ) function GetArray($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } function GetAssoc($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } I searched a lot but didn't find anything. Thanks for any help :) You're welcome. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... Hi Rob, Thanks for your help, its associative but I forget to mention its needs To be a single dimensional associative array. $result = array(); while ( $row = mysql_fetch_assoc($get) ) { $result[] = $row; } This creates multi dimensional like: Array ( [0] = Array ( [0] = 3 [1] = BMW ) [1] = Array ( [0] = 1 [1] = Mercedes ) ) I tried to play with foreach and array_push but still not perfect Thanks, Andras -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql_fetch_array to associative array
On Mon, 2007-07-16 at 13:37 -0700, Andras Kende wrote: Hi Rob, Thanks for your help, its associative but I forget to mention its needs To be a single dimensional associative array. $result = array(); while ( $row = mysql_fetch_assoc($get) ) { $result[] = $row; } This creates multi dimensional like: Array ( [0] = Array ( [0] = 3 [1] = BMW ) [1] = Array ( [0] = 1 [1] = Mercedes ) ) I tried to play with foreach and array_push but still not perfect I'm not sure I understand... you have multiple rows being returned do you not? How to you intend to handle them in a single level array? Let's image the following rows are returned from the database: array ( 'title' = 'The Dragonbone Chair', 'author' = 'Tad Williams', ), array ( 'title' = 'Sword of Shannarah', 'author' = 'Terry Brooks', ), Show me how you would like the final array to look and I can figure out the code you need to organize it the way you want. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql_fetch_array to associative array
-Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Monday, July 16, 2007 1:46 PM To: Andras Kende Cc: php-general@lists.php.net Subject: RE: [PHP] mysql_fetch_array to associative array On Mon, 2007-07-16 at 13:37 -0700, Andras Kende wrote: Hi Rob, Thanks for your help, its associative but I forget to mention its needs To be a single dimensional associative array. $result = array(); while ( $row = mysql_fetch_assoc($get) ) { $result[] = $row; } This creates multi dimensional like: Array ( [0] = Array ( [0] = 3 [1] = BMW ) [1] = Array ( [0] = 1 [1] = Mercedes ) ) I tried to play with foreach and array_push but still not perfect I'm not sure I understand... you have multiple rows being returned do you not? How to you intend to handle them in a single level array? Let's image the following rows are returned from the database: array ( 'title' = 'The Dragonbone Chair', 'author' = 'Tad Williams', ), array ( 'title' = 'Sword of Shannarah', 'author' = 'Terry Brooks', ), Show me how you would like the final array to look and I can figure out the code you need to organize it the way you want. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... Hi Rob, $conn-getassoc(select id,name from cars) I would need a single array like: (its for used with pear:quickform dropdown) Array ( [2] = BMW [1] = FORD ) Or: Array ( [The Dragonbone Chair] = 'Tad Williams' [Sword of Shannarah] = 'Terry Brooks' ) Thanks a lot, Andras -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql_fetch_array to associative array
On Mon, 2007-07-16 at 13:37 -0700, Andras Kende wrote: function GetAssoc($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } I'll focus on the above one since that's probably what's causing your grief... I'm also going to rename it so it's more explanatory: ?php function GetPairs( $query, $keyField, $valueField ) { $pairs = array(); if( ($get = $this-Execute ( $query )) ) { while( ($row = mysql_fetch_assoc( $get )) ) { $pairs[$row[$keyField]] = $row[$valueField]; } mysql_free_result( $get ); } return $pairs; } ? That's the function... it creates an array of pair mappings based on the table fields $keyField and $valueField. So let's say you have the following table in your database: CREATE TABLE cars ( id int not null auto_increment, namevarchar( 32 ) not null, primary key( id ) ); Then you might issue the following function call: ?php $query = SELECT id, name FROM cars ORDER BY name ASC ; $pairs = GetPairs( $query, 'id', 'name' ); ? Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql_fetch_array to associative array
-Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Monday, July 16, 2007 6:12 PM To: Andras Kende Cc: php-general@lists.php.net Subject: RE: [PHP] mysql_fetch_array to associative array On Mon, 2007-07-16 at 13:37 -0700, Andras Kende wrote: function GetAssoc($query) { $get = $this-Execute ( $query ); $result = array(); while ( $row = mysql_fetch_array($get) ) { $result[] = $row; } mysql_free_result($get); return $result; } I'll focus on the above one since that's probably what's causing your grief... I'm also going to rename it so it's more explanatory: ?php function GetPairs( $query, $keyField, $valueField ) { $pairs = array(); if( ($get = $this-Execute ( $query )) ) { while( ($row = mysql_fetch_assoc( $get )) ) { $pairs[$row[$keyField]] = $row[$valueField]; } mysql_free_result( $get ); } return $pairs; } ? That's the function... it creates an array of pair mappings based on the table fields $keyField and $valueField. So let's say you have the following table in your database: CREATE TABLE cars ( id int not null auto_increment, namevarchar( 32 ) not null, primary key( id ) ); Then you might issue the following function call: ?php $query = SELECT id, name FROM cars ORDER BY name ASC ; $pairs = GetPairs( $query, 'id', 'name' ); ? Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Rob, THANKS I also hacked together a version: while ( $row = mysql_fetch_array($get) ) { $result[$row[0]] = $row[1]; } Best regards, Andras -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php