tags:

views:

252

answers:

4

Hello. I'm working with PHP 5 here. I have the following code:

$data = json_decode($_POST['data']);
foreach ($data as $obj) {
......

}

I get the error "Invalid argument supplied for foreach()" on the line with the foreach function. This only happens on my shared server account. On my local webserver everything works fine. $_POST['data'] contains valid JSON string. print_r($data); shows nothing...What the hell is wrong here ?

EDIT: It really blows my mind but the $_POST['data'] string is being sent with AJAX and I catch the string with FireBug and copied into a JSON test file like this:

$data = json_decode('[{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"}]');
print_r($data);

And it comes out as it should, properly. So I'm gonna paste more code from the function that troubles:

function saveData($table)
{
    $data = json_decode($_POST['data']); 

    $db = new MySQL(true);
    $db->TransactionBegin();

    foreach ($data as $obj) {
  $id = $obj->id;
  $name = $obj->name;
}
}

Check this out: If I do this:

$data = json_decode('[{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"}]');
foreach ($data as $obj) {
......

}

Works perfect! So there seems to be e problem when I do:

$data = json_decode($_POST['data']);

But $_POST['data'] outputs a perfect JSON string. It's driving me crazy.

Any ideas ?

Any ideas ?

A: 

json_decode is only present on PHP 5 >= 5.2.0 with PECL json >= 1.2.0, so my guess is that your hosting is using a lower version.

rogeriopvl
Wouldn't that cause an error (indeed a fatal error) for calling an undefined function before even reaching foreach?
eyelidlessness
That would throw a fatal error on `json_decode()` not existing.
chaos
I had this situation before, and the PHP (on a shared hosting too) had the json_encode function, but not the PECL and didn't gave an error. I didn't had access to the logs, so I got no certainty of the issue.
rogeriopvl
Edited post. Please check.
Manny Calavera
A: 

$data is invalid JSON? (Either malformed or empty)

eyelidlessness
+1  A: 

var_dump($data) will presumably show that $data is null. From the docs:

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

If the behavior is special to your shared server, perhaps it has a lower recursion limit than your local server.

chaos
Edited post. Please check.
Manny Calavera
Okay. Not seeing anything about your edit that affects my answer.
chaos
+2  A: 

magic_quotes_gpc... Quote from php.net:

When on, all ' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically.

As it turns out magic quotes is enabled on the shared server. Thank you for your answers.

Manny Calavera
Magic Quotes enabled on the shared server...
jeroen
Thank you Jeroen
Manny Calavera