简体   繁体   中英

Cannot output json from MySQL - getting null value

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~ :) 好快!

it would appear that $row['something'] is returning a null value for every row. Make sure you've got proper data to output.

Do a var_dump($row) inside your loop to see what your database is returning. It will output the contents of each row. Post the results here. We can't diagnose the problem with dummy values like you put in your question. I'm going to make a bet that you're trying to select a column from the table that doesn't exist. Post your exact code.

Try array_push($somethings, $row['something']);

Also, it is better to use ajax to retrieve data, than to directly assigning to a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM