0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Where is your android code? Are you getting problem in android or php? – Pratik Butani Jul 20 '16 at 07:07
  • Where is client? May be He/She is on another PC? **You are calling from `localhost`** – Pratik Butani Jul 20 '16 at 07:08
  • What does `var_dump($jsonobj)` give you...? – deceze Jul 20 '16 at 07:09
  • Try checking your database credentials again by trying to manually login. Also a database name of `general db` should have no spaces, change it to `general_db` – Glenn Dayton Jul 20 '16 at 07:09
  • BTW, don't learn `mysql`. Forget about that. Don't use any tutorial that describes it. It's deprecated and practically dead. Learn PDO instead. – deceze Jul 20 '16 at 07:10
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Jul 20 '16 at 09:35
  • Try `json_last_error_msg()` and you will get something more meaningful that a number – RiggsFolly Jul 20 '16 at 09:38
  • how you are running your client? I think run your client in browser with this link http://localhost/bhavil/WebServices/JsonServices/JsonEg1_client.php – Rakesh Kumar Jul 20 '16 at 09:46
  • @Pratik Bhutani : I'm getting error on both. but my assumption is that clearing it out on the serverside will fi the problem on android as well. Both the client and the server are on the same pc. – Bhavil Jain Jul 21 '16 at 09:09
  • @deceze : It gives me the entire code of the server. I know pdo is better and i'm trying to get into practice of using it.. thanks for the advise none the less :). – Bhavil Jain Jul 21 '16 at 09:15
  • @RakeshKumar : Yes, Thats how im running the client side. – Bhavil Jain Jul 21 '16 at 09:15
  • @everyone: I Finally got fed up and reverted to wamp 2.0. and everthing is running properly again. but it surely would be great to have an answer to this problem. While trying to find a solution. I realized that there was a massive change between 2.0 and 2.5. In 2.5 you had to create virtual hosts for each project and i was using alias to access my projects. so i tried the virtual host thing and my wamp broke and then things got more screwed as my new installation didn't have my DBs and everything just went heywired... so i just downgraded. didnt get my DBs back bt atleast the code is running – Bhavil Jain Jul 21 '16 at 09:17

0 Answers0