PHP returns HTML tags along with JSON in WAMP
I want to return the values stored in a table in MySQL in JSON form. I wrote the following PHP code as shown below: -
json1.php
<?php
header('Content-type:application/json');
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('testdb');
$select = mysql_query('select * from questions');
$rows = array();
while($row=mysql_fetch_array($select))
{
$rows[] = $row;
}
echo json_encode($rows);
?>
When I check this in my browser I get the following output: -
<br />
<font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\unit1\json1.php on line <i>5</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\unit1\json1.php' bgcolor='#eeeeec'>..\json1.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>242688</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
( )</td><td title='C:\wamp\www\unit1\json1.php' bgcolor='#eeeeec'>..\json1.php<b>:</b>5</td></tr>
</table></font>
[{"0":"Test1","product":"Test1","1":"Hello","questions":"Hello"},{"0":"","product":"","1":"","questions":""},{"0":"Test1","product":"Test1","1":"Venky","questions":"Venky"},{"0":"","product":"","1":"","questions":""}]
As you can see url returns json along with a bunch of html tags. I do not want it. I just want the json to be returned.
I am using Windows 7 with WAMP Server. I put my php file in the "C: \ wamp \ www \ unit1" directory. Oddly enough, when I use the same php file in my MAC with the MAMP server, the output (in the browser) is just a json string (just the way I want it).
Could you help me figure out what is wrong with my Windows system? Is there a way to just return a json string (no html tags) from WAMP?
Please, help.
source to share
Did you even read the warning?
Deprecated: mysql_connect (): the mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Tells you exactly what is wrong, you are using mysql_ * functions which are deprecated, switch to prepared mysqli or PDO statements instead.
Documentation links: Prepared Statements and PDOs
source to share
In the php settings, the error display mode is enabled, which is incorrect.
Typically, these warnings will appear as readable text in the browser window. This may not work if you sent the result as a json content type.
Disable the display of errors in the submitted result and just log it to the log file. which makes much more sense and doesn't disturb your output. You can do this setting in your php config, usually in the central file php.ini
.
The reason you are getting errors (or warnings in that case) is simply that there is something wrong with your PHP code. You are using an outdated and outdated mysql connection driver. Switch to a new one like mysqli (or PDO) to prevent security issues. Read about the benefits of "prepared statements" for this.
source to share
This helped me too. It seems that "display errors" are set to 'on' by default when wamp is installed. It was for me anyway. I think that when your new to PHP and the tools you use when developing with PHP (like me) stupid problems like this can be frustrating. It's hard to find the right answer when you don't even know the right question.
So, if any other noobs (like me) are looking for this:
on the taskbar, click the release notification icon.
select PHP / PHP options and uncheck Display Errors.
as stated earlier, you must send your errors to a log file.
source to share