PHP Webservice is failing to return the encoded response accurately

A piece of PHP code that I developed successfully registers user details from my iPhone app and returns JSON output. Here is the code snippet:

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();

However, when attempting to check if a username already exists before adding it to the database table, an error occurs as indicated in the Xcode Log.

JSON text did not start with array or object and option to allow fragments not set.

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";
  }


}

Answer №1

There seems to be a php error present in the json output, possibly just a notification or warning depending on your error_reporting settings.

To resolve this issue, it is important to debug the problem and carefully assess the error_reporting level for production environments.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

php using array_diff

I need help finding the difference between two arrays. $array1 = array(1,2,3,4); $array2 = array(1,2,3,4,5); $difference = array_diff($array1, $array2); print_r($difference); Why am I getting no output? Shouldn't the result be "5" included in th ...

Attempting to access the nested JSONObject within another JSONObject

I have this JSON I generated: { "error":false, "0":{ "tvInfo":{ "id":"0", "name":"The War of Thrones", "type_id":"1", "score":"8.1", "votes":"780", "created_date":"2011-04-17", ...

Reading an irregular JSON file with AngularJS if statement

I received a json data with the following structure: [ { "level" : "1", "name" : "TIER 1 - CORE FOUNDATIONS", "required" : "13", "completed" : "9", "planned" : "0", "subcategories" : [ { "level" : "2", ...

A comprehensive guide on transferring user input onto a php text file

I am trying to implement a feature in my PHP code where the contents entered in a text box are uploaded to a text file. However, I only want them to be written if the string contains a specific pattern. Here is the modified PHP code: <?php $data = $_ ...

Remove a product using its product_id with PHP and MySQL

I am encountering an issue where the product id is not being passed correctly for deletion from the database. When I click on the delete button, it seems to be deleting the wrong products in sequence instead of the one I intended to remove. The form is set ...

When hovering over slick text, it becomes hidden because of non-responsive CSS. Are there any solutions to make it responsive?

I can't seem to get the on-hover dates to appear, even though they are rendered when inspecting the page. I suspect it could be related to a responsive CSS issue or class breaking. How can I resolve this? https://i.stack.imgur.com/jyEJb.png https:// ...

PHP and AJAX can sometimes cause variable values to become lost

I've been encountering an issue while attempting to validate an email using ajax. For some reason, the mail variable seems to disappear during the process. Despite thoroughly checking for typos multiple times, I can't seem to locate any errors. T ...

Using JSON in Android to Load Data and Display in a ListView

I've encountered an issue with the following code that is meant to read a JSON file and display it in a listview - it's returning an empty list. Any advice would be greatly appreciated. Thanks! eventList.json: { "events": [ { "event": "Taste o ...

When outputting the $http response in the console, Angular displays null instead of the expected result,

I have encountered a peculiar issue with my local webservice developed using Spring. Everything seems to be functioning correctly when accessing it through the browser or Postman, but for some reason, when attempting a simple GET method with Angular/Ionic, ...

Locate a character inside quotation marks by utilizing preg_replace

Here is a string that needs editing: 106;"LORDAL La Manicure, ser;um do manuzi, wzrost, 5 ml";301*04*334;46;"loksx-la-mainusttej-wzroxst-5-ml";"";"";19.51220;"";"";"thumb23_9671ds359.png" The task is to replace semicolons (;) within paired quotes. In t ...

Dealing with RxSwift observable errors interrupting the chain of events - Utilizing Rx for web services: strategies for

As a newcomer to RxSwift, I have extensively studied various documentations and talks but still feel like I am lacking some fundamental concepts. In my app, I encounter a challenge where the base URL of a RESTful web service is not known at build or start ...

ngRepeat does not iterate through an array

Presented below is an object: { "_id" : ObjectId("5454e173cf8472d44e7df161"), "questionType" : 0, "question" : "question1", "answers" : [ { "content" : "answer1" }, { "is_true" : "true", ...

Adding a key-value pair to an associative array using a foreach loop in PHP

When working with JSON data obtained from an API endpoint, I have a requirement to insert a key-value pair into an array. Below is the function I am using: $magazines = Magazine::all(); foreach ($magazines as $magazine) { $resul ...

Mapping fields in Spring to JSON can be tricky, especially when dealing with fields that can be

In my object, there is a String value field which can contain either true or false. The issue arises when Spring/Jackson maps this to value: "true" as a String, causing problems with certain angularjs ng-model mappings that expect a boolean. Is there a way ...

Transferring the link value to an AJAX function when the onclick event is triggered

I have a link containing some data. For example: <li><a href="" onclick="getcategory(this);"><?php echo $result22['category']; ?></a></li> I need this link to pass the value of $result22['category']; to ...

Mapping an array of values in Retrofit for Android: A step-by-step guide

The API endpoint will provide a JSON response structured as follows: { "students":[ [ 1, "Tom", 18, "USA" ], [ 2, "Linda", 21, "Mexico" ] ], "other":[ ...

When utilizing an iframe, the file surpasses the specified ini size

I am currently using fancy box iframe to showcase my page, containing a file upload button. However, when I click on the form submit button, I encounter the error message "File exceeds the defined ini size". Despite searching through various resources on G ...

Error occurred while trying to call android.app.Service() without any arguments provided

In my experience with using Retrofit to fetch data from an API, I encountered a peculiar issue. The response would fail only when the service Array contained data; if it was null, the response would be successful and all other data would be present. Howeve ...

Tips for fixing the issue "Uncaught SyntaxError: Unexpected token S in JSON at position 0"

After referencing sols of SO, I am still unable to solve the error. I have a file dashboard.html which contains search conditions. When clicked, it calls loadtable.js. This loadtable.js file uses search.php to retrieve rows from a table. However, encount ...

A step-by-step guide to receiving Json input in a CakePhp Restful API through the PUT method

Managing data through passing IDs in URL format like /apis/view/id.json allows me to view and delete data. public function view($id) { $api = $this->Api->findById($id); $this->set(array( 'api' => $api, ...