0

I have a php web service that takes the request from android/iphone app and uses the input to find the appropriate data from the mysql database. The result is encoded back to json. How can I return the json txt/msg to the android/iphone app. Please help?????

asked Mar 25, 2012 at 9:07

2 Answers 2

1
echo (json_encode ($data_to_send));
answered Mar 25, 2012 at 9:09
3
  • You forgot to set the content type. Commented Mar 25, 2012 at 9:12
  • this code will only convert the database result(in array) to json. I want to return the json txt to the corresponding mobile. The mobile app is used simultaneously by many users. Commented Mar 25, 2012 at 9:14
  • @NeerajDangol That's how you do it. (plus sending the right content headers as Tomalak said). Echoing sends to the client. Commented Mar 25, 2012 at 9:18
0

short answer : Use json_encode();

echo json_encode($data);

long answer :

Don't forget headers :

header('Content-type: application/json');

Beware of errors, don't echo them, or echo them in json with json_encode() So, you need to configure PHP (or at least this page) :

  • not to show errors and to log them
  • handle errors as array or directly in json
answered Mar 25, 2012 at 9:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.