AdRock Posted March 16, 2010 Share Posted March 16, 2010 Is it possible to get all column names and values into an associative array from a mysql query. The reason i ask is that i don't want to have to write $var = $row['var'] $var2 = $row'[var2'] $var3 = $row['var3'] $var4 = $row'[var4'] for every field in the database. i would rather be able to have an array where i just use the element such as echo $array[0]; Link to comment https://forums.phpfreaks.com/topic/195502-associative-arrays-from-mysql-query/ Share on other sites More sharing options...
Andy-H Posted March 16, 2010 Share Posted March 16, 2010 Yes, look into extract. example: $row = mysql_fetch_assoc($result); extract($row, EXTR_PREFIX_INVALID, '_'); echo $var . ' ' . $var2 . ' ' . $var3 . ' ' . $var4; //if array key is invalid for varname i.e.($row['1example']) echo $_1example; Hope that helps. Link to comment https://forums.phpfreaks.com/topic/195502-associative-arrays-from-mysql-query/#findComment-1027330 Share on other sites More sharing options...
AdRock Posted March 16, 2010 Author Share Posted March 16, 2010 Many thanks It works and saves so much code Link to comment https://forums.phpfreaks.com/topic/195502-associative-arrays-from-mysql-query/#findComment-1027343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.