0

Display.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>

Trying to alert data.0.user.

0

2 Answers 2

4

The problem is that you can't have a . followed by a number (save in the context of a Number). You need to use Array look-up syntax.

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

On the other hand:

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

Also, a free-standing digit followed by a . needs to be followed by another digit (or a non-variable character). (eg: a0 can be followed by a ., as can 1, but 1. must be followed by a digit)

Sign up to request clarification or add additional context in comments.

2 Comments

+1, but I don't understand your last statement. Because 1. as valid as 1.0 or 1.
@IAbstractDownvoteFactory I meant contiguous variable character
2

Use data[0].user in your Javascript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.