1

我希望我自己的表名包含在 JSON 中,并且不想使用 mysql 表名。如果我重命名该列,我只需在 de php 文件中重命名它们。

简而言之:

  • 运行查询
  • 获取mysql
  • 从输出重命名列
  • 编码为 JSON

这就是我现在所拥有的:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $rows['feed'] =  $row;
    sendResponse(200, json_encode($rows));
}

我想知道:如何打开获取的数组并更改列名并将它们重命名为我自己的名称,然后将它们发送到 JSON

编辑:我编辑了我的行

 $result = mysql_query("SELECT * FROM od_common.debiteur WHERE SORT_NAAM LIKE '%comp%'");



while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$deb_nr = $row['DEB_NR'];
$deb_naam = $row['DEB_NAAM'];
$deb_adres = $row['DEB_ADRES'];



$rows['klant'] = array('klantnr' => $deb_nr, 'klntnm' => $deb_naam, 'adrs'      =>         $deb_adres);





sendResponse(200, json_encode($rows));
}
}

我几乎得到了我想要的。但我想要这个:

 {
   "klant": [
    {
      "klntnr": "10010",
      "klntnm": "Company1",
  "adrs": "street1"
},
{
  "klntnr": "25071",
  "klntnm": "Company2",
  "adrs": "street2"
},
{
  "klntnr": "25247",
  "klntnm": "Company3",
  "adrs": "street3"
},
{
  "klntnr": "25454",
  "klntnm": "Company4",
  "adrs": "street4"
},
{
  "klntnr": "25601",
  "klntnm": "Company5",
  "adrs": "street5"
}
  ]
 }

不是这个:

{  "klant": {
 "klantnr": "10010",
 "klntnm": "Company1",
 "adrs": "street1"
   }
  }{
  "klant": {
    "klantnr": "25071",
    "klntnm": "Company2",
    "adrs": "street2"
  }
 }{
  "klant": {
    "klantnr": "25247",
    "klntnm": "Company3",
    "adrs": "street3"
  }
 }{
   "klant": {
    "klantnr": "25454",
    "klntnm": "Company4",
    "adrs": "street4"
  }
 }{
  "klant": {
    "klantnr": "25601",
    "klntnm": "Company5",
    "adrs": "street5"
  }
 }
4

1 回答 1

5

最好的办法是从源头修改您的查询,例如:

$result = mysql_query("SELECT columna, columnb FROM table");

至:

$result = mysql_query("SELECT columna AS 'whatever', column AS 'ha' FROM table");

下一个最好的想法是修改$row数组,但这是非常基本的 PHP。

于 2013-05-16T11:54:11.887 回答