2013-05-08 80 views
-1

即使所有使用Google搜索,我都无法获得此权限。Jquery获得mysql值作为数组

我有一个MySQL表,我需要从一个列中的值返回到我的jQuery脚本是这样的:

array = 123, 556, 553, 542, 4322... 

我要么得到这样的:

array = "123""556""553""542""4322"... 

或像这样

[{"FB_ID":"123"}, {... 

我该如何用php或jquery实现?

继承人我的当前代码,结果看起来像最后一个。

$connection = mysql_connect("$host", "$username", "$password") or die(mysql_error()); 

$sql = "SELECT FB_ID FROM `database`.`players` WHERE recieved = '1'"; 
$result =mysql_query($sql); 


$rows = array(); 
while($r = mysql_fetch_assoc($result)) { 
    $rows[] = $r; 
    } 

echo json_encode($rows); 

回答

0

尝试铸造为int:

$rows[]=(int)$r['FB_ID']; 
1

如果你想这样的:

[123, 556, 553, 542, 43222, ...] 

做到这一点:

$rows = array(); 
while($r = mysql_fetch_assoc($result)) { 
    $rows[] = $r['FB_ID']; 
} 
1

需要指定一个键获取这是价值。

while($r = mysql_fetch_assoc($result)) { 
    $rows[] = $r['FB_ID']; 
} 
0

在你的PHP页面

header("Content-type: application/json"); 
$arr = array(123, 556, 553, 542, 4322); 
echo json_encode($arr); 

而在jQuery的

$.ajax({ 
url:'yoururl', 
type:'post', 
dataType:'json', 
success:function(result){ 
    var response = JSON.stringify(result); 
    res    = JSON.parse(response); 
    console.log(res); 
} 
});