2015-05-11 24 views
0

我正在讀取數據庫中的值,並且希望將值存儲爲會由代碼重用的會話對象。但是,代碼執行得很好,但不會在會話存儲中存儲任何值。無法存儲來自json對象的會話值

JS代碼

$.getJSON("sessionstore.php", {"points": myjson}, function(data){ 
      sessionStorage.user = data[0]; 
      sessionStorage.usermail = data[2]; 
      sessionStorage.supmail = data[4]; 
      sessionStorage.repsupervisor = data[3]; 
      sessionStorage.issupervisor = data[5]; 
    }); 

php code 

<?php 
include 'mydbconn.php'; 

if (isset($_POST["points"])) { 
$points = json_decode($_POST["points"]); 
} 
// decode the json data that is being passed 
$usrname = $points->usrname; 

    $sql = "Select EmpName, EmpMail, RepSupervisor, SupMail, IsSupervisor from glusers where EmpName='$usrname'"; 
    $result = $conn->query($sql); 
    $data = array(); 

while($row = $result->fetch_assoc()){ 
    $data[] = $row; 
}; 
echo $data[0]; 
die(); 
?> 

有沒有錯誤,但代碼不執行。

Regards,

回答

0

您不輸出有效的json。請注意,$data[0]是你的結果集中的一行 - 一個數組 - 所以當你在echo它在PHP中會輸出類似Array的東西。

你需要改變:

echo $data[0]; 
die(); 

到:

echo json_encode($data[0]); 
die(); 

編輯:此外,你fecthing關聯數組,所以你需要解決他們在你的JavaScript爲:

data.EmpName 
data.EmpMail 
// etc. 
+0

對不起,它是回聲json_econde($ data); –

+0

@ArjunBhandari看我的編輯。 – jeroen