PHP在WAMP中返回HTML标签和JSON

时间:2015-01-13 17:57:45

标签: php html json wamp

我想以JSON的形式返回存储在MySQL表中的值。我写下面的PHP代码如下: -

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

?>

当我在浏览器中查看此内容时,我得到以下输出: -

<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":""}]

如您所见,url返回json以及一堆html标记。我不想要这个。我只想让json返回。

我正在使用Windows 7和WAMP Server。我把我的php文件放在&#34; C:\ wamp \ www \ unit1&#34;目录中。奇怪的是,当我在MAMP服务器的MAC中使用相同的php文件时,输出(在浏览器中)只是json字符串(完全按照我想要的方式)。

你能帮我弄清楚我的windows系统有什么问题吗?有没有办法从WAMP返回json字符串(没有html标签)?

请帮忙。

5 个答案:

答案 0 :(得分:4)

你有没看过警告?

  

不推荐使用:mysql_connect():不推荐使用mysql扩展,将来会删除它:使用mysqli或PDO代替

告诉你到底出了什么问题,你正在使用不推荐使用的mysql_ *函数,转而使用mysqli预处理语句或PDO。

指向文档的链接:Prepared statementsPDO

答案 1 :(得分:1)

您在php设置中启用了错误显示,这是错误的。

因此,这些警告通常会在浏览器窗口中显示为可读文本。如果您将结果作为json内容类型发送,这可能不起作用。

禁用所发送输出中的错误显示,而只是将其记录到日志文件中。这更有意义,不会打破你的输出。您可以在php配置中进行调整,通常在中心php.ini文件中。

你得到错误(或在这种情况下是警告)的原因只是你的PHP代码有问题。您正在使用过时且已弃用的mysql连接驱动程序。切换到更新的类似mysqli(或PDO)以防止出现安全问题。阅读&#34;准备好的陈述的优点&#34;对此,也是。

答案 2 :(得分:1)

你有:

  • 已安装并启用Xdebug
  • 将html_errors设置为on

答案 3 :(得分:0)

这也使我绊倒了一会儿。安装wamp时,默认情况下“显示错误”设置为“打开”。反正对我来说。我想当你刚开始使用PHP时,以及使用PHP开发时使用的工具(就像我一样),像这样的愚蠢问题可能令人沮丧。当你甚至不知道正确的问题时,很难找到正确的答案。

所以如果有任何其他新手(像我一样)正在搜索这个:

在任务栏中单击wamp通知图标。

选择PHP / PHP设置并取消选中“显示错误”设置。

如前所述,您应该将错误发送到日志文件中。

答案 4 :(得分:-2)

禁用已弃用的声明 - 将此代码放在php文件的开头

<?php
error_reporting(E_ALL & ~E_DEPRECATED);