2

i have a json data retrieved from data base i.e

$result=array();
$query="SELECT * FROM fish";
$result1 = mysql_query($query, $conn);
while ($table = mysql_fetch_array($result1, MYSQL_ASSOC)){
 $result[]=$table;
}
 echo json_encode($result);
and this give me the result
[{"fish_id":"1","name":"first fish update","info":"this is my first fish update","image":"http:\/\/www.localhost\/cafe\/pics\/logout (1).gif"}]

but from another page when i call this json data i.e

$input = file_get_contents("http://localhost/fish/fish-json.php");
$json=json_decode($input);
echo $json->fish_id;

it give me the error

Notice: Trying to get property of non-object in /var/www/fish/json-to-php.php on line 13 Call Stack: 0.0005 318764 1. {main}() /var/www/fish/json-to-php.php:0
asked Dec 21, 2010 at 13:04
2
  • Does $input contain any result when you echo it? Commented Dec 21, 2010 at 13:06
  • print_r() or var_dump() is your friend :)) Commented Dec 21, 2010 at 13:12

5 Answers 5

5

Is an array of object, so

echo $json[0]->fish_id;

To loop

if (!is_array($json)) die('...');
foreach ($json as $key=>$fish)
{
 echo $fish->fish_id;
}
answered Dec 21, 2010 at 13:10
1
  • hay thanks,it works but if there are more than one records then how could i iterate to this Commented Dec 21, 2010 at 13:18
0

Try

$input = file_get_contents("http://localhost/fish/fish-json.php");
$json=json_decode($input);
echo $json['fish_id'];
answered Dec 21, 2010 at 13:07
0

try echo $json['fish_id']; i might suspect it convert it to an array

answered Dec 21, 2010 at 13:08
0

try this first:

echo $json;

to check if you got valid json. Eventually, the php in fish-json.php was not executed.

answered Dec 21, 2010 at 13:11
0

By default json_decode returns an stdClass object. You have to give a second parameter for this function TRUE.

$json=json_decode($input, TRUE);
echo $json[0]['fish_id'];
answered Dec 21, 2010 at 13:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.