从PHP Web服务不返回正确的编码响应

从PHP Web服务不返回正确的编码响应,php,ios,json,xcode,afnetworking,Php,Ios,Json,Xcode,Afnetworking,我已经编写了简单的PHP代码来注册iphone应用程序中的用户详细信息。它工作正常并返回JSON输出。我在下面添加了该代码 header('Content-type: application/json'); include 'connection.php'; $response = array(); $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; if($use

我已经编写了简单的PHP代码来注册iphone应用程序中的用户详细信息。它工作正常并返回JSON输出。我在下面添加了该代码

header('Content-type: application/json');
include 'connection.php';
$response = array();

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

if($username == NULL || $password == NULL || $email == NULL ){

    $response["success"] = 0;
    $response["message"] = "Something Empty";

}else{

    $sql = "INSERT INTO User (username, password, email)
VALUES ('".$username."', '".$password."', '".$email."')";

if ($conn->query($sql) === TRUE) {

    $response["success"] = 1;
    $response["message"] = "Done";

} else {

    $response["success"] = 0;
    $response["message"] = "Error";
}
}

echo json_encode($response);
$conn->close();
但当我尝试在添加到表之前检查用户名是否已存在时。我从我的Xcode日志中得到错误信息

JSON文本没有以数组或对象开头,并且没有设置允许片段的选项。

if($username == NULL || $password == NULL || $email == NULL ){

    $response["success"] = 0;
    $response["message"] = "Something Empty";

}else{

   $query = mysql_query("SELECT * FROM User WHERE username='".$username."'");


    if (mysql_num_rows($query) != 0)
  {
      $response["success"] = 0;
      $response["message"] = "Username Already Exists";

  }else{

      $response["success"] = 1;
      $response["message"] = "That Name Fine";
  }


}

很可能是php错误(可能只是一个通知或警告,具体取决于您的
error\u reporting
设置)污染了json输出


你需要调试这个问题,你可以考虑<代码> Error报告生产级别。<代码> $Que= MySqLyQuy(“从用户选择用户名=”“$UrrordNo.``”);
如果(mysql\u num\u rows($query)!=0)
您使用的是mysql还是mysqli连接,那么在调用
之前会输出一些东西吗?那就是您的错误。您已经使用了
mysql\u query()
Awesome。是的,它解决了问题。我根据mysqlimysql被折旧的原因更改了代码,所以您肯定应该使用mysqli。mysqli还支持更多选项和功能