views:

82

answers:

3

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  A: 

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

Ty W
so what should i put in? any suggest?
sky
check what the fieldnames are in the 'posts' table. Obviously there's 3 records being returned, but you're looking for the wrong names. if there isn't a `something` field in the returned row, PHP will happily auto-munge the variable to 'null' and assign that to your $somethings array. It's bad practice to do 'select *'. Don't be lazy and always specify the field name(s) you want to retrieve.
Marc B
A: 

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

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

Natkeeran
This is just a different way of writing what the author already has. It won't solve the problem.
mattbasta
A: 

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.

mattbasta