即时通讯使用以下从php中检索字符串,我想知道如何使我的字符串成为一个数组。把.get json字符串变成数组?
jQuery的
$.get("get.php", function(data){
alert(data);
//alert($.parseJSON(data));
}, "json");
注释掉部分似乎没有任何效果,所以我真的不能告诉我在做什么错了,可能有人请指教?
如果需要,我可以发布PHP。
谢谢。
PHP
<?php
$username="root";
$password="root";
$database="testing";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$name= $_GET['name'];
$query="SELECT * FROM tableone ";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$array = array();
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"firstname");
$last=mysql_result($result,$i,"lastname");
$date=mysql_result($result,$i,"date");
$ID=mysql_result($result,$i,"id");
$array[$i] = $first;
$i++;
}
echo json_encode($array);
?>
输出:
["James","Lydia","John"]
返回什么数据? – j08691 2012-04-13 15:42:25
由于您已经提供'json'作为返回dataType'data'已经是您可以使用的对象,您不需要'$ .parseJSON(data)'。这假设你的PHP页面以正确的格式返回数据。 – 2012-04-13 15:42:45
请发布生成JSON的PHP代码;你需要改变的是PHP代码,而不是JavaScript - 数据是PHP代码返回的JSON。 – ThiefMaster 2012-04-13 15:42:55