我的php页面生成一个没有表名的json

时间:2015-07-02 10:43:08

标签: php json

我试图从这里列出的php页面生成一个简单的json对象:

{ 
        "personaggio" : 
            [{  
                "Nome":"Rafael",
                "Password":"chimeratech",
                "Gold":"1100",
                "Food":"2",
                "Pf":"96",
                "Date":"0875-06-18",
                "Exp":"178000"
              },
              {
                "Nome":"Vandermax",
                "Password":"leechaolan",
                "Gold":"200",
                "Food":"3",
                "Pf":"145",
                "Date":"0875-06-18",
                "Exp":"126000"
               }
            ]
    }``

我期待这样的事情:

   [
        {  
            "Nome":"Rafael",
            "Password":"chimeratech",
            "Gold":"1100",
            "Food":"2",
            "Pf":"96",
            "Date":"0875-06-18",
            "Exp":"178000"},
            {"Nome":"Vandermax",
            "Password":"leechaolan",
            "Gold":"200",
            "Food":"3",
            "Pf":"145",
            "Date":"0875-06-18",
            "Exp":"126000"
        }
    ]``

但我得到的是:

deparse(yourFunction)

所以我的桌名" personaggio"缺少...我怎样才能正确获得预期的json文件?

1 个答案:

答案 0 :(得分:0)

SELECT查询中你得不到表名,如果你想在结果中也要表名,你需要将表名保存在变量中并将结果分配给表名变量

例如: -

    mysql_connect("******","*******","") or die("Errore connessione al sito");
    mysql_select_db("*******") or die("Errore selezione database");
    $table_name = "personaggio"; // assign the table_name to variable
    $output = array(); // assign empty array for output variable
    $q=mysql_query("SELECT * FROM ". $table_name);
    while($e=mysql_fetch_assoc($q))
        $output[$table_name][]=$e; // now collect the result array in table_name array

    print(json_encode($output)); // your expected output
    mysql_close();

您将获得结果数组保存在 table_name

中的预期输出