2011-09-06 66 views
0

Display.php问题用的getJSON或PHP

<?php 
include 'sql.php'; 
$dataget = mysql_query("SELECT `user`,`message`,`timestamp` FROM `messages`"); 
$arr = array(); 
while ($dataarr = mysql_fetch_assoc($dataget)){ 
    $arr[] = $dataarr; 
} 
echo json_encode($arr); 
?> 

Index.php

<script type="text/javascript" src="../jquery.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $.getJSON('display.php', function(data) { 
      alert(data.0.user); 
     }); 
    }); 
</script> 

试图提醒data.0.user

回答

4

问题是你不能有一个.后跟一个数字(保存在一个数字的上下文中)。您需要使用数组查找语法。

var o = {0:"foo"} 
o.0 // SyntaxError 

在另一方面:

var o = {0:"foo"} 
console.log(o[0])//foo 

此外,一个独立的数字,后面接着.需要被接着另一个数字(或不可变的字符)。 (如:a0可以跟一个.,如可以1,但1.必须跟一个数字)

+0

+1,但我不明白你最后的陈述。因为'1.'与'1.0'或'1'一样有效。 – Joe

+0

@IAbstractDownvoteFactory我的意思是连续变量字符 – cwallenpoole

2

使用data[0].user在你的JavaScript。