Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I want to store all of my MySQL results in an array I can manipulate with javascript/jQuery.

Here is my current code:

<?php
    $sql = "SELECT * FROM potentials";
    $result = mysql_query($sql) or die(mysql_error());
    $potential = mysql_fetch_array($result);
    echo json_encode($potential);
?>

And my Javascript:

<script type="text/javascript">
    $(document).ready(function(){
        var myArray = "<?php print(json_encode($potential)); ?>";
        console.log(myArray)
    )};
</script>

I keep getting "Unexpected number" or "unexpected identifier". Whats going on?

share|improve this question
    
it echos out fine, but doesnt console.log in the javascript. –  Rick Bross Apr 14 '13 at 15:31

3 Answers 3

up vote 2 down vote accepted

json_encode() returns an object using JSON notation. Strangely, you surrounded it with quotes. Try removing them :

<script type="text/javascript">
$(document).ready(function(){
    var myArray = <?php print(json_encode($potential)); ?>;
    console.log(myArray);
});
</script>

(and it's not an array, it's an object, so you might want to rename your variable ;) )

share|improve this answer
    
That did it thanks! –  Rick Bross Apr 14 '13 at 15:36
    
Its now spitting out only the first row of potentials. How to I get all rows? a multidemensional array? what would that look like? maybe a foreach statement? –  Rick Bross Apr 14 '13 at 15:39
    
I would go for something like this : <?php $sql = "SELECT * FROM potentials"; $result = mysql_query($sql) or die(mysql_error()); $potential = array(); while($row = mysql_fetch_assoc($result)) { $potential[] = $row; } echo '<script type="text/javascript"> $(document).ready(function(){ var myArray = "' . json_encode($potential) . '"; console.log(myArray) )}; </script>' . "\n"; ?> –  Über Apr 14 '13 at 16:34
1  
Note: replace )}; with }); at line 5 of the answer to prevent 'unexpected token )' bug in console. –  hexicle Feb 5 '14 at 8:18

You are using json_encode() twice, however if you want to use it, you need to parse it. In jQuery this can be done via jQuery.parseJSON like this

var myArray = jQuery.parseJSON('<?php print json_encode($potential); ?>');

Also if you want all the results, you need to loop the query (e.g. with while) and then save it to an array

$array = array();
while ($row = mysql_fetch_array( mysql_query( $query ) )
{
    $array[] = $row;
}
share|improve this answer
    
That gives me this in the console: Uncaught TypeError: Object function (E,F){return new o.fn.init(E,F)} has no method 'parseJSON' –  Rick Bross Apr 14 '13 at 15:34
<script>
    var data = '<?php echo $data; ?>';
    var json = JSON.parse(data);
    console.log(json[0]); //etc
</script>

Notice that var data = ... is SINGLE QUOTED, so you catch the echo from php as a String

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.