Home > DeveloperSection > Forums > PHP script returning JSON not working
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    May-02-2013 3:43 AM

 PHP PHP 
Ratings:


 1 Reply(s)
 1107  View(s)
Rate this:
Hi Guys

The below returns "\nQuery was empty" as I run it simply from my server with the URL in the Browser.

This is the PHP code:

    <?

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

?>

Thanks 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-02-2013 8:26 AM

Hi Pravesh!

There is no need to use file_get_content and you have to put one white space after table name.

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

Don't want to miss updates? Please click the below button!

Follow MindStick