I need simple web service to get data from local database, but when I've moved files from test server to actual server I just receive blank page with 'null' in top left corner. My php code is very simple:
<?php
// Include config
require_once 'config/db.php';
$sql = new db();
$conn = $sql->connect();
$task = isset($_GET['task']) ? mysql_real_escape_string($_GET['task']) : "";
if(!empty($task))
{
if($task === "totals")
{
$qur = mysql_query("working query - no problem here;");
$result =array();
while($r = mysql_fetch_array($qur))
{
extract($r);
$result[] = array("total_value" => $total_value, 'orders_date' => $orders_date, 'number_of_orders' => $number_of_orders);
}
$json = $result;
}
}
@mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
?>
And here is db.php code:
<?php
class db
{
// Properties
private $dbhost = 'ip-address';
private $dbuser = 'xxx';
private $dbpass = 'yyy';
private $dbname = 'zzz';
// Connect
public function connect()
{
$conn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
mysql_select_db($this->dbname, $conn);
}
}
?>
May be Json_encode not giving correct answer.
1) Also please make sure JSON_PRETTY_PRINT
is only available for PHP versions >= 5.4. It's value is 128, so try replacing JSON_PRETTY_PRINT with 128.
just on error reporting at top of page to debug
2) Also to make sure once try to print $json
before encoding it
3) As mentioned in comment just encode only when you are actually getting data
!empty($json){
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.