PHP 在 WAMP 中返回 HTML 标签和 JSON

Par*_*try 5 html php 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);

?>
Run Code Online (Sandbox Code Playgroud)

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

<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":""}]
Run Code Online (Sandbox Code Playgroud)

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

我正在使用带有 WAMP 服务器的 Windows 7。我已将 php 文件放在目录“C:\wamp\www\unit1”中。奇怪的是,当我在 MAC 中使用与 MAMP 服务器相同的 php 文件时,输出(在浏览器中)只是 json 字符串(正是我想要的方式)。

你能帮我找出我的 Windows 系统出了什么问题吗?有没有办法只从 WAMP 返回 json 字符串(不带 html 标签)?

请帮忙。

小智 4

你读过警告吗?

已弃用:mysql_connect():mysql 扩展已弃用,将来将被删除:使用 mysqli 或 PDO 代替

告诉您到底出了什么问题,您正在使用 mysql_* 函数,该函数已弃用,请改用 mysqli 准备好的语句或 PDO。

文档链接:准备好的语句PDO