Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how can I get column value by name from a row?

Tags:

php

mysql

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.

like image 723
Joeblackdev Avatar asked Nov 23 '25 09:11

Joeblackdev


2 Answers

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

like image 80
Parris Varney Avatar answered Nov 24 '25 23:11

Parris Varney


You need to use mysql_fetch_assoc to get the associative array.

like image 26
borrible Avatar answered Nov 24 '25 23:11

borrible



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!