Joeblackdev
Joeblackdev

Reputation: 7327

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"];

Where description is the name of the column whose value I want to print? Can this be done?

Thank you very much.

Upvotes: 2

Views: 16090

Answers (3)

Parris Varney
Parris Varney

Reputation: 11478

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

Upvotes: 10

borrible
borrible

Reputation: 17346

You need to use mysql_fetch_assoc to get the associative array.

Upvotes: 3

Glen Solsberry
Glen Solsberry

Reputation: 12320

You want to use the mysql_fetch_assoc function instead.

Upvotes: 2

Related Questions