PHP how can I get column value by name from a row?
I'm a bit stuck on this one. In my PHP code, I query for some records which are returned. I loop through the rows, but for each row I want to retrieve a column value using its name as an index.
while ($row=mysql_fetch_row($result))
{
echo $row[7];
}
This prints out what I want, but I wanted to do something like:
echo $row["description"开发者_StackOverflow社区];
Where description is the name of the column whose value I want to print? Can this be done?
Thank you very much.
You could also use this:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
Then you can switch back to numeric with MYSQL_NUM
or both with MYSQL_BOTH
You need to use mysql_fetch_assoc to get the associative array.
You want to use the mysql_fetch_assoc function instead.
精彩评论