RE: [PHP] mysql_fetch_array to associative array

From: Date: Mon, 16 Jul 2007 20:37:30 +0000
Subject: RE: [PHP] mysql_fetch_array to associative array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-258953@lists.php.net to get a copy of this message
-----Original Message----- From: Robert Cummings [mailto:robert@interjinn.com] 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

« previous php.general (#258953) next »