Forum Moderators: coopster

Message Too Old, No Replies

Easy way to assign column names as array keys?

PHP/MySql

         

HughMungus

3:47 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Want to create an array that has db column names for the key names and field values as the array values. Is there an easy way to do this? If not possible directly, is there a way in php/mysql to get a list of column names in a table such that I could use those values as the keys?

mincklerstraat

8:17 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Answer to first question is mysql_fetch_assoc - [be2.php.net...] .

$query = 'SELECT * FROM yourtablename'; 
$query .= ' WHERE id="456"'; /* add a WHERE line if you just need a few rows and not the whole shebang */
$result = mysql_query($query);
if(!$result) echo '<br />query was '.$query.'<br />error is '.mysql_error();
else {
$row = array();
$i = 0;
while($row = mysql_fetch_assoc($result)){
echo '$row['.$i.']:<pre>';
print_r($row[$i]);
echo '</pre>';
}
}
echo 'Now you\'ve got your array $row and you can do all sorts of groovy stuff with it.'