JSON_encode在浏览器中输出正确,而print(json)在Swift中输出'nil'


JSON_encode outputs correctly in browser, yet print(json) in Swift outputs 'nil', why?

我有一个问题,我的JSON被输出为NIL在XCode,即使当我在浏览器中测试它报告正确的值。

$player1Id = array();
$player1Id = getPlayer1Id($player1name,$communityId);
public function getPlayer1Id($playerName, $communityId)
{
$returnValue = array();
$sql = "SELECT users.id'n"
. "FROM users'n"
. "join community_players'n"
. "on community_players.player_id=users.id 'n"
. "WHERE users.user_name = '".$playerName."' AND community_players.community_id = '".$communityId."'";
$result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1 )){
        $row = $result -> fetch_array(MYSQLI_ASSOC);
        if(!empty($row)){
            $returnValue = $row['id'];
        }
    }
    return $returnValue;
}
echo json_encode(array($player1Id));

如果我在浏览器中使用URL中的一组值进行测试,它会正确输出:

("31")

这是我的Swift函数:
func submit(action: UIAlertAction){
//    print (homePlayerLabel.text!,":",homeTeamLabel.text!,homeGoals,"(",player1result!,") - ", awayPlayerLabel.text!,":",awayTeamLabel.text!,awayGoals,"(",player2result!,")")

    // Send user data to server side
    let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/submitScore.php");
    var request = URLRequest(url:myUrl!);
    request.httpMethod = "POST";
    let postString = "communityId='(communityId!)&player1name='(homePlayerLabel.text!)&player1team='(homeTeamLabel.text!)&player1goals='(homeGoals)&player1result='(player1result!)&player2name='(awayPlayerLabel.text!)&player2team='(awayTeamLabel.text!)&player2goals='(awayGoals)&player2result='(player2result!)";
    print (postString)
    request.httpBody = postString.data(using: String.Encoding.utf8);

    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

        DispatchQueue.main.async
            {

                if error != nil {
                    print("error='(error)")
                    return
                }
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
                    print (json)
                    if let parseJSON = json {
                        let returnValue = parseJSON["status"] as? String
                        if( returnValue == "Success")
                        {
                            let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert);

                            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(action) in
                                self.dismiss(animated: true, completion: nil)
                            }
                            myAlert.addAction(okAction);
                            self.present(myAlert, animated: true, completion: nil)
                        } else {

                            let errorMessage = parseJSON["message"] as? String
                            if(errorMessage != nil)
                            {
                                self.displayMyAlertMessage(userMessage: errorMessage!);
                            }
                        }
                    }
                } catch{
                    print(error)
                }

        }
    }
    task.resume()

}

然而,当我在Xcode中的print (json)报告为:

有没有什么明显的东西是我忽视了的?

我认为你更新一个php到7.1我得到同样的问题,而更新php到7.1 try to put mysql_query('SET CHARACTER SET utf8')我认为这是解决你的问题