检索MySQL行密钥

检索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?

我想从MySQL数据库中检索一行,例如,使用相应的键返回一行。 现在,在打印时,检索行只是在PHP中给出一个数组: p>

  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] =>如此美味
 [7] => 5 
 [8] => 35.282753 
 [9] => -120.659615  
)
  code>  pre> 
 
 

如何检索以便密钥是数据库中字段的名称,这样当我去使用json_encode函数时,密钥 与值? p> div>相关联

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