php-mysqli\u fetch\u assoc to json编码数组适用于大多数查询,但不适用于其他查询(尽管查询返回有效结果)

cngwdvgl  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(291)

代码如下:

if ($fetch = mysqli_fetch_array($result0)) {
  //Found a company - now show all maintenances for company
  $company_id = $fetch[0];
  $result = mysqli_query($dbhandle, "SELECT maintenancedb.maileingang, maintenancedb.bearbeitetvon, maintenancedb.startDateTime, maintenancedb.done, maintenancedb.id FROM maintenancedb WHERE maintenancedb.lieferant LIKE '$company_id'") or die(mysqli_error($dbhandle));

  if ($resultsrows = mysqli_fetch_assoc($result)) {
    $array2 = array();

    while ($resultsrows = mysqli_fetch_assoc($result)) {
      $array2[] = $resultsrows;
      //var_dump($resultsrows);
    }
    echo json_encode($array2);
  }else {
    $jsonArrayObject = array(array('maileingang' => 'no maintenances for this lieferant in system.', 'bearbeitetvon' => '', 'startDateTime' => '', 'done' => '', 'id' => ''));
    echo json_encode($jsonArrayObject);
  }
} else {
  $jsonArrayObject = array(array('maileingang' => 'no such company in DB yet', 'bearbeitetvon' => '', 'startDateTime' => '', 'done' => '', 'id' => ''));
  echo json_encode($jsonArrayObject);
  exit;
}

因此第一个示例返回一个有效的json数组。有4行结果。var_dump将每一行作为一个数组输出,然后在底部是json编码的数组,该数组显示在一个表中。

array(5) {
  ["maileingang"]=>
  string(19) "2018-11-21 14:46:43"
  ["bearbeitetvon"]=>
  string(7) "abcde"
  ["startDateTime"]=>
  string(19) "0000-00-00 00:00:00"
  ["done"]=>
  string(1) "0"
  ["id"]=>
  string(2) "16"
}
array(5) {
  ["maileingang"]=>
  string(19) "2018-11-23 06:52:00"
  ["bearbeitetvon"]=>
  string(7) "abcde"
  ["startDateTime"]=>
  string(19) "2019-01-15 22:00:00"
  ["done"]=>
  string(1) "0"
  ["id"]=>
  string(2) "17"
}
array(5) {
  ["maileingang"]=>
  string(19) "2018-11-21 14:46:00"
  ["bearbeitetvon"]=>
  string(7) "abcde"
  ["startDateTime"]=>
  string(19) "2018-11-13 11:00:00"
  ["done"]=>
  string(1) "1"
  ["id"]=>
  string(2) "18"
}
array(5) {
  ["maileingang"]=>
  string(19) "2018-11-25 12:08:00"
  ["bearbeitetvon"]=>
  string(7) "abcde"
  ["startDateTime"]=>
  string(19) "2018-11-27 05:00:00"
  ["done"]=>
  string(1) "1"
  ["id"]=>
  string(2) "19"
}
[{"maileingang":"2018-11-21 14:46:43","bearbeitetvon":"abcde","startDateTime":"0000-00-00 00:00:00","done":"0","id":"16"},{"maileingang":"2018-11-23 06:52:00","bearbeitetvon":"abcde","startDateTime":"2019-01-15 22:00:00","done":"0","id":"17"},{"maileingang":"2018-11-21 14:46:00","bearbeitetvon":"abcde","startDateTime":"2018-11-13 11:00:00","done":"1","id":"18"},{"maileingang":"2018-11-25 12:08:00","bearbeitetvon":"abcde","startDateTime":"2018-11-27 05:00:00","done":"1","id":"19"}]

现在是json数组返回完全空的示例。只返回了一行(正确),但不知何故json编码似乎失败了:

array(5) {
  ["maileingang"]=>
  string(19) "2018-12-07 15:33:00"
  ["bearbeitetvon"]=>
  string(7) "abcde"
  ["startDateTime"]=>
  string(19) "2018-12-12 13:00:00"
  ["done"]=>
  string(1) "0"
  ["id"]=>
  string(2) "23"
}
[]

知道为什么吗?似乎没有任何非法字符等,当我创建数据库连接顺便说一句,我已经声明 mysqli_set_charset($db, 'utf8'); 编辑:哦,我注意到第一个查询实际上应该返回5个结果,但是只返回4个。。但是,在我意外地从0或1开始的地方没有for循环。

vhmi4jdf

vhmi4jdf1#

我明白了-对于那些后来偶然发现的人:
内部if语句和while语句是问题所在:

if ($resultsrows = mysqli_fetch_assoc($result)) {
    $array2 = array();

    while ($resultsrows = mysqli_fetch_assoc($result)) {
      $array2[] = $resultsrows;
      //var_dump($resultsrows);
    }
    echo json_encode($array2);
  }else {
    $jsonArrayObject = array(array('maileingang' => 'no maintenances for this lieferant in system.', 'bearbeitetvon' => '', 'startDateTime' => '', 'done' => '', 'id' => ''));
    echo json_encode($jsonArrayObject);
  }

第一个if语句检查 $resultsrows = mysqli_fetch_assoc($result) 检查了0结果,因此相同的while检查从结果1开始。
这就是为什么结果0(在只有1的情况下)根本没有被编码,而在多于1的情况下,就像在我的第一个例子中有5个结果,其中只有4个被显示,第一个没有被编码。

相关问题