我在這裏使用PHP 5。我有以下代碼:在共享服務器上爲「foreach()提供的無效參數」
$data = json_decode($_POST['data']);
foreach ($data as $obj) {
......
}
我得到錯誤「的foreach爲無效的論點提供()」與在foreach功能就行了。這隻發生在我的共享服務器帳戶上。在我的本地網絡服務器上一切正常。 $ _POST ['data']包含有效的JSON字符串。的print_r($數據);什麼也沒有顯示......這裏到底怎麼了?
編輯:這真的吹我的腦海裏,但$ _ POST [「數據」]字符串正在使用AJAX發送和我趕上與螢火串並複製到一個JSON測試文件是這樣的:
$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);
它應該是正確的。所以,我會貼得更代碼從功能的煩惱:
function saveData($table)
{
$data = json_decode($_POST['data']);
$db = new MySQL(true);
$db->TransactionBegin();
foreach ($data as $obj) {
$id = $obj->id;
$name = $obj->name;
}
}
檢查了這一點:如果我這樣做:
$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) {
......
}
運行完美!因此,似乎有通過電子郵件問題時,我做的事:
$data = json_decode($_POST['data']);
但$ _ POST [「數據」]輸出一個完美的JSON字符串。這讓我瘋狂。
任何想法?
任何想法?
在共享服務器上啓用魔術引號... – jeroen 2009-09-26 01:32:15