检索MySQL行密钥

I want to retrieve a row from a MySQL database which returns a row, for example, with the corresponding key. Right now, retrieving a row simply gives an array in PHP when printed as such:

Array
(
    [0] => 15
    [1] => 2011-02-27 22:31:46
    [2] => salmon-with-pineapple-curry-sauce-photo_0.jpg
    [3] => 1
    [4] => Salmon Fillet
    [5] => Trader Joes
    [6] => So delicious
    [7] => 5
    [8] => 35.282753
    [9] => -120.659615
)

how would I retrieve so that the key is the name of the field in the database so that when I go to use the json_encode function, the key is associated with the value?

Look at mysql_fetch_assoc() which returns the rows as an array indexed with DB table field

Output array will looks like this

Array
(
    ['id'] => 15
    ['date'] => 2011-02-27 22:31:46
    ['name'] => salmon-with-pineapple-curry-sauce-photo_0.jpg
    .....
    .....//other elements or fields
)

This function is probably what you are looking for: http://php.net/manual/en/function.mysql-fetch-assoc.php

You can use mysql_fetch_array with MYSQL_ASSOC option, or mysql_fetch_assoc...

http://php.net/mysql_fetch_array

http://php.net/mysql_fetch_assoc

Use the mysql_fetch_assoc to get an associated array with the results. That should give you what you want.

Link : http://www.php.net/manual/en/function.mysql-fetch-assoc.php