1

I am new to PHP... I have been trying to encode the values retrieved from a MySQL database in JSON and then display the results on a browser..

mysqlcn.php

mysql_connect("127.0.0.1","root","chetan");
mysql_select_db("db1");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output,JSON_FORCE_OBJECT));
mysql_close();

When I try to run it in a browser using address http://localhost/mysqlcn.php, I get this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fcharset0 
Times New Roman;}{\f1\fswiss\fcharset0 Arial;}} {\*\generator Msftedit 
5.41.15.1507;}\viewkind4\uc1\pard\sb100\sa100\f0\fs24\par \par \f1\fs20\par
\par }

I am currently using WAMP server with PHP 5.3.0

SQL schema

CREATE TABLE people (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 100 ) NOT NULL ,
sex BOOL NOT NULL DEFAULT '1',
birthyear INT NOT NULL)
5
  • 1
    Use the application/json header when you're outputting JSON to the page. Commented Feb 26, 2011 at 5:00
  • 2
    That's not the output this script could generate by itself. Did you try not using Internet Explorer? Commented Feb 26, 2011 at 5:00
  • @russell dias...could u plzzz elaborate ur point by giving an example.. Commented Feb 26, 2011 at 6:49
  • @chetan: header("application/json"); followed by your json encoded data. Commented Feb 26, 2011 at 6:54
  • yes i tried this...still not showing the output Commented Feb 26, 2011 at 8:26

2 Answers 2

2

Your output appears to be RTF. Most likely, you have some code which is converting it from HTML to RTF.

Sign up to request clarification or add additional context in comments.

1 Comment

yup u were correct...i then wrote the program in notepad++ and saved it...on running the program apache server crashed....dont know why???
0

Why do you wish to use JSON to do the display of your database?

PHP can handle this on its own just fine, unless you have a specific reason.

1 Comment

actually i am developing an android application where the client retrieves the data from server...i will be parsing data using json...

Your Answer

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.