返回JSON的PHP脚本不起作用

时间:2013-05-02 05:50:45

标签: php json

下面返回“\ nQuery为空”,因为我只是从我的服务器运行它,并在浏览器中使用URL。

这是PHP代码:

    <?

$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM  `Tailor`LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input"); 
$sth = mysql_query($query);

if (mysql_errno()) { 
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'\n';
    echo mysql_error(); 
}
else
{
    $rows = array();
    while($r = mysql_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    print json_encode($rows);
}
?>

3 个答案:

答案 0 :(得分:1)

无需使用file_get_content,您必须在表名后添加一个空格。

<?php

$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM  `Tailor` LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
 mysql_select_db($databasename) or die(mysql_error());

$sth = mysql_query($query);

if (mysql_errno()) { 
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error(); 
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
}
print json_encode($rows);
}
?>

答案 1 :(得分:0)

您的查询有一个问题,您需要在tablename

之后添加空格
SELECT * FROM  `Tailor` LIMIT 0 , 30

答案 2 :(得分:0)

第一

$query = "SELECT * FROM  `Tailor`LIMIT 0 , 30";

但你覆盖它

$query = file_get_contents("php://input"); 

// $query like   aaa=ggg&bbb=kkk

然后你

$sth = mysql_query($query);

// LIKE $sth = mysql_query('aaa=bbb&ccc=lll'); //it not sql query format