【问题标题】:Null values returned in JSON response to query even though no null values present in database即使数据库中不存在空值,在 JSON 响应中返回空值以响应查询
【发布时间】:2015-05-30 02:11:21
【问题描述】:

我想使用 php 获取上述数据,我将电子邮件作为参数传递,我想检索所有具有 user = email 的记录。然而,我得到的回应有点奇怪。我主要关心的是奇怪的格式和空值的存在。我确定我在某处的代码中有一个小错误,但是由于我对 php 不是很熟悉,所以我发现很难识别错误。输出如下所示:

{"0":"5phd6ure1lj941cv81o00tb4jr","id":null,"1":"8","stake":"8","2":"tester@tester.com","user":"tester@tester.com","3":"29","returns":"29","4":"(9\/X)","teams":"(9\/X)","5":"open","status":"open","bet":{"stake":null,"user":null,"returns":null,"teams":null,"status":null}} 

这是我使用的两个文件:

Get_Bets.php

<?php

class Get_Bets {

   private $db;

   function __construct() {

	require_once 'DB_Connect.php';
	$this->db = new DB_Connect();


	$this->db->connect();


}


function __destruct() {
   
  }

  public function getUsersBets($email) {

   $conn=mysqli_connect("****", "***", "***","***");

   $result = mysqli_query($conn,"SELECT id,stake,user,returns,teams,status FROM bet WHERE user = '$email'");

   $no_of_rows = mysqli_num_rows($result);
     
    if ($no_of_rows > 0) {
     
      $result = mysqli_fetch_array($result);

      return $result;
       
    }
}
}
?>

Get_All_Bets.php

<?php

if (isset($_POST['email']) && $_POST['email'] != '') {
    // get tag
    $email = $_POST['email'];
 
    // include db handler
     require_once 'include/Get_Bets.php';


    $db = new Get_Bets();
 
    // response Array
    $response = $db->getUsersBets($email);
            $response["id"] = $bet["id"];
            $response["bet"]["stake"] = $row["stake"];
            $response["bet"]["user"] = $row["user"];
            $response["bet"]["returns"] = $row["returns"];
            $response["bet"]["teams"] = $row["teams"];
            $response["bet"]["status"] = $row["status"];
    echo json_encode($response);
}
?>
 

【问题讨论】:

    标签: php jquery mysql json database


    【解决方案1】:

    Get_All_Bets.php 中,此代码段导致错误

    $response["id"] = $bet["id"];
    $response["bet"]["stake"] = $row["stake"];
    $response["bet"]["user"] = $row["user"];
    $response["bet"]["returns"] = $row["returns"];
    $response["bet"]["teams"] = $row["teams"];
    $response["bet"]["status"] = $row["status"];

    删除它,输出会正常

    现在让我们讨论发生了什么
    $response = $db-&gt;getUsersBets($email); 这一行之后,$response 变量看起来像这样:

    {
      "0": "5phd6ure1lj941cv81o00tb4jr",
      "id": "5phd6ure1lj941cv81o00tb4jr",
      "1": "8",
      "stake": "8",
      "2": "tester@tester.com",
      "user": "tester@tester.com",
      "3": "29",
      "returns": "29",
      "4": "(9\/X)",
      "teams": "(9\/X)",
      "5": "open",
      "status": "open"
    }

    在错误代码 $response["id"] = $bet["id"]; 的第 1 行,您正在用新值覆盖 $response 数组中的键 id,这是不行的,因为之前没有定义名称为 $bet 的变量所以 @987654330 @ 会给你一个 null,这就是为什么 id 条目为 null,同样适用于条目 $response["bet"],没有先前定义的名为 $row 的变量,这就是为什么所有条目都为 null

    P.S:在mysqli_fetch_array 中,您可以传递另一个指示获取模式的参数,如果您不需要数字条目,只需像 mysqli_fetch_array($result, MYSQLI_ASSOC); 那样调用它

    【讨论】:

    • 非常感谢您的详细回答,如果我想返回所有带有 user = email 的记录,到目前为止它只返回第一个,但表中有 3 个应该返回
    • 进行这些更改后,我得到以下输出: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
    • $row= mysqli_fetch_array($result) 返回结果集中的下一行,获取所有需要的记录 循环直到mysqli_fetch_array 返回null,代码类似于$all_rows = array(); while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $all_rows []= $row; } return $all_rows;
    • 循环遍历整个结果集的详细示例here
    • 我在您的问题中看不到与 {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":n‌​ull} 相关的任何内容,可能是这个 new 问题的另一个详细问题是好的:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多