This is my first question on stackoverflow, so i apologize in advance if its been asked and answered before. I tried searching but could not find an answer, or maybe i don't know the exact terms to use.
I'm current learning PHP and Android and recently got onto the topic of webservices (in both of the languages). I made a basic JSON webservice using php but its not working properly. Below is the code
JSON Server :
<?php
$hostname="localhost";
$username="root";
$pass="";
$dbname="general db";
$connect=mysql_connect($hostname,$username,$pass);
mysql_select_db($dbname,$connect) or die("DB not Found");
$query=mysql_query("select * from products");
$jsonobj= array();
while($result=mysql_fetch_object($query))
{
$jsonobj[]=$result;
}
$final=json_encode($jsonobj);
echo $final;
?>
Json Client :
<?php
$jsonobj=file_get_contents('http://localhost/Bhavil/WebServices/JsonServices/JsonEg1_server.php');
$final=json_decode($jsonobj,true);
var_dump($final);
echo json_last_error();
?>
The output I get when I run the server is :
[
{"ID":"1","Name":"Sunglasses","Price":"1000"},
{"ID":"2","Name":"Mobile Phone","Price":"40000"},
{"ID":"3","Name":"Mouse","Price":"5000"},
{"ID":"4","Name":"Pen","Price":"10"}
]
This is the data stored in the table.
But when I run the client I get null as the output :
D:\Program Files\Wamp Server\www\bhavil\WebServices\JsonServices\JsonEg1_client.php:5:null
and Json_last_error() returns '4'.
I dont know what i'm doing wrong, I've checked it a hundred times and tried too many things but nothing works.
Other Details:
OS: Windows 10.
PHP Version : 5.5.12
Apache Version : 2.4.9
Using Wamp Server 2.5 for testing