0

I'm using following code but cannot return data from MySQL.

This is the output:

<script type="text/javascript"> 
    var somethings= [null,null,null]; 
</script> 

It does have three post, but I couldn't get the title(message) output.

EDIT: this is the code I'm using:

<?php

    $session = mysql_connect('localhost','name','pass');     
    mysql_select_db('dbname', $session);    

    $result= mysql_query('SELECT * FROM posts', $session); 
    $somethings= array(); 
    while ($row= mysql_fetch_assoc($result)) { 
        $somethings[]= $row['something']; 
    } 
?> 

<script type="text/javascript"> 
    var somethings= <?php echo json_encode($somethings); ?>; 
</script> 

This is the table:

message

Try iPhone post!

Welcome to Yo~ :)

好快!

1 Answer 1

3

Why did you use $row['something'] in the loop body? You don't need this.

Try such code:

$somethings = array(); 
while ($row = mysql_fetch_assoc($result)) { 
    $somethings[] = $row;
}
Sign up to request clarification or add additional context in comments.

2 Comments

yep, it works out fine, thanks, just one more question, how i get the result as : var somethings= {"Welcome to Yo~ :)"},{"Try iPhone post!"},{"message":"????"}; instead of var somethings= [{"message":"Welcome to Yo~ :)"},{"message":"Try iPhone post!"},{"message":"????"}]; with chinese character support?
json_encode() works with UTF-8 encoded data. Maybe you have problems with MySQL connection, which doesn't work in the UTF-8? Try to put additional query before the "SELECT" query: mysql_query("SET NAMES 'utf8'");

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.