Jump to content

associative arrays from mysql query


AdRock

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.