我想以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服务器。我已经将我的php文件放在目录“C:\wamp\www\unit1 1”中。奇怪的是,当我用MAMP在我的MAC中使用相同的php文件时,输出(在浏览器中)只是json字符串(这正是我想要的方式)。
你能帮我找出我的窗户系统出了什么问题吗?有没有一种方法只从WAMP返回json字符串(没有html标记)?
请帮帮忙。
发布于 2015-01-13 18:01:46
发布于 2015-01-13 18:01:24
在php设置中启用了错误显示,这是错误的。
通常,这些警告将在浏览器窗口中显示为可读的文本。如果您以json内容类型发送结果,这可能无法工作。
禁用发送输出中的错误显示,只需将其记录到日志文件中即可。这样做更有意义,而且不会破坏您的输出。您可以在php配置中进行这种调整,通常是在中央php.ini文件中。
您获得错误(或在本例中为警告)的原因很简单,就是您的php代码有问题。您正在使用过时的、不受欢迎的mysql连接驱动程序。切换到一个新的,如mysqli (或PDO),以防止安全问题。也请阅读“准备好的陈述”的优点。
发布于 2015-01-13 18:01:37
你有:
onhttps://stackoverflow.com/questions/27928450
复制相似问题