我有一个问题,我的JSON在XCode中作为NIL输出,即使我在浏览器中测试它报告正确的值。
$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)
报告为:
NIL
我有什么明显的东西在看吗?
答案 0 :(得分:0)
我认为您将 php更新为7.1 我将更新php更新为7.1 try to put mysql_query('SET CHARACTER SET utf8')
我遇到同样的问题我觉得它可以解决您的问题