我運行mysql查詢併成功獲取結果。但是,我無法從JavaScript端讀取數組的元素。誰能幫忙?如何將php數組轉換爲javascript數組
//JAVASCRIPT makes a request
function profiles(){
$.post('dbConn.php', { opType:"getProfileList" }, fillProfileCombo, "text");
}
function fillProfileCombo(res) {
alert(res);
}
//dbConn.php takes the request , gets the result and passes via echo as it is shown as follows:
//RETURN PROFILE LIST
else if (!strcmp($opType, "getProfileList")){ //no param is coming
$connect = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$profiles = mysql_query(" SELECT DISTINCT profileName FROM `map_locations` ");
$row = mysql_fetch_array($profiles);
/*while() {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
*/
//$data = array();
//$row = mysql_fetch_assoc($profiles)
/*while($row = mysql_fetch_assoc($profiles))
{
$data[] = $row;
}*/
if ($row){
echo $row;
} else {
echo "ERROR occured";
}
}
//問題:
//當我改變回聲$行; into echo $ row [0];,我看到一個警告框的第一個元素...查詢肯定是工作..
//但是當我改變資源水庫[0],它不會顯示任何東西 - 這是正常的,因爲我不知道如何將php數組轉換爲js數組..
function fillProfileCombo(res) {
alert(res[0]); // does not work..
}
我不想用json的方式......我不是很擅長。我不想搞砸了。任何建議和幫助表示讚賞。
爲什麼你不希望使用JSON?這是使用JavaScript代表數據結構的最簡單方法之一 – Phil 2010-10-26 00:14:11
你能給我一個關於這段代碼的例子嗎? – Ozlem 2010-10-26 00:17:22