PDA

View Full Version : JSON decoding problem


alinulms
06-19-2009, 02:18 PM
Hello coders,

I have a problem to which i need imediate help. Here it is.


<?php

$conn = mysql_connect('localhost:8889','root','root') or die (mysql_error ());
$db = mysql_select_db ('PHPTest') or die (mysql_error ());

$result=mysql_query ('SELECT * FROM cars') or die (mysql_error ());

$data = array();

while ($row=mysql_fetch_object($result))
{
$data [] = $row;
}

echo json_encode($data);


$receivedData = $_GET['data'];
$string = str_replace("[","'",$receivedData);
$string1 = str_replace("]","'",$string);
$obj = json_decode(stripslashes($string1),true);

$file = fopen("test.txt","w");

fwrite($file,$obj->id);

fclose($file);


?>


This is my file which reads some data from DB sends it to a js which modifies it and then encodes in jbos sends back. Thing is i want to acess the data field by field. the $obj variable has the value '{"id":2,"model":"Cayenne","brand":"Porscheasd","year":"2008","color":"Gr"}'

and i can write it to a file.

But if i want to use $obj->id ... writes noting.
If i do $obj[1] it writes {

What am i doing wrong and how can i acess the elements id, model, brand etc?

Pls help as soon as u can