MySQL/PHP:如何获取列和关联数据 [英] MySQL/PHP: How to get columns and associated data
问题描述
我知道我可以使用以下命令从数据库中获取列/字段名称:SHOW COLUMNS FROM tablename
...但是有一种方法可以获取返回的列以及内容,所以我的返回值是像这样的东西:
I know I can get the column/field names from a database by using: SHOW COLUMNS FROM tablename
...but is there a way I can get the columns returned as well as the content, so my return would be something like this:
array [0] [0] =列1名称
array [0] [1] =列2名称
array [0] [2] =第3列名称
array[0][0] = column 1 name
array[0][1] = column 2 name
array[0][2] = column 3 name
array [1] [0] =第1列第1列的值
array [1] [1] =第2列第1行的值
array [1] [2] =第3列第1行的值
array[1][0] = column 1 row 1 value
array[1][1] = column 2 row 1 value
array[1][2] = column 3 row 1 value
...
我正在寻找一个JSON对象
I'm looking to build a JSON object
jsonObj = {
项目1 {
nbsp; bsp 第1列名称:第1列第1行的值,
; 第2列名称:第2列第1行的值,
nbsp; bsp 第3列名称:第3列第1行的值...
jsonObj = {
item 1 {
column 1 name: column 1 row 1 value,
column 2 name: column 2 row 1 value,
column 3 name : column 3 row 1 value...
我应该补充一点,我知道字段名称与查询结果一起返回
I should add that I know the field names are returned with the query results
(选择* FROM sometable),如果您使用
(SELECT * FROM sometable) and if you use
while ($row = mysql_fetch_array($results)) {
$thisfield = $row['fieldname'];
}
但是,当我不知道它们时如何访问它们(字段名称)?
But how can I access them (the field names) when I do not know them?
推荐答案
使用mysql_fetch_assoc-它将返回
use mysql_fetch_assoc - it will return
array("name" =>"value","name2" =>"value2" ...);
array( "name" => "value", "name2" => "value2" ... );
然后使用json_encode($ data)
then use json_encode( $data )
这篇关于MySQL/PHP:如何获取列和关联数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!