都可以分成單獨的數組格式JSON字符串由PHP腳本返回? 我的JS代碼如何解析JSON數組?
function GetData(id){
$.ajax({
url: 'list.php',
data: 'id=' + id,
type: 'POST',
success: function(data){
alert(data)
console.log(data)
}
})
}
我list.php的
<?php
$id = $_POST['id'];
$connect = mysql_connect("localhost", "admin", "118326") or die(mysql_errno());
$db = mysql_select_db("flabers", $connect) or die(mysql_error());
$result = mysql_query("SELECT * FROM list WHERE id = '$id'") or die(mysql_error());
$array = mysql_fetch_assoc($result) or die(mysql_errno());
echo json_encode($array);
?>
返回JSON字符串
{"id":"88","country":"Korea, Japan, USA","brand":"Samsung, Sony, HTC"}
如何得到兩個數組的國家和品牌?
'data.country'?這不是一個數組,但只是一個字符串。 – deceze
您正在使用[過時的數據庫API](http://stackoverflow.com/q/12859942/19068),並應使用[現代替換](http://php.net/manual/en/mysqlinfo.api。 choosing.php)。你也暴露了自己[SQL注入攻擊](http://bobby-tables.com/),一個現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-防止 - 注入 - 在PHP中)自己從。 – Quentin
我知道,我只是無法創建陣列的問題,因爲它已經(但它不適用於主題) – Dima