Im having a little confusion why the following is not working.
get.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[] = $first;
$i++;
}
echo json_encode($array);
?>
jQuery
var arr = new Array();
$.get("get.php", function(data){
arr = data;
alert(arr);
}, "json");
When I run the following I get a list of names that looks like this
["James","Lydia","John"]
But when I try to single out an entry such as arr[2], i am give just a 'J', why is it that the elements arnt single entries like I would expect?
Can anyone lend a hand?
Thanks!
Update
$.get("get.php", function(data){
arr = $.parseJSON(data);
alert(arr);
}, "json");
does not seem to return results?
arr
is still a string. Otherwise,alert(arr)
would outputJames, Lydia, John
. – Felix Kling Apr 13 '12 at 15:07var arr = new Array();
doesn't do anything, since you'll assign another value to that variable afterwards anyway. – Shedal Apr 13 '12 at 15:09