简体   繁体   English

从MySQL使用JSON获取Null值,如何将数据从MySQL正确检索到JSON?

[英]Getting Null value with JSON from MySQL, how to retrive data from MySQL to JSON correctly?

I'm using following code but cannot return data from MySQL. 我正在使用以下代码,但无法从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. 它确实有三个帖子,但我无法获得title(message)输出。

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! 尝试iPhone发布!

Welcome to Yo~ :) 欢迎来到哟〜:)

好快! 好快!

Why did you use $row['something'] in the loop body? 为什么在循环主体中使用$ row ['something']? You don't need this. 你不需要这个

Try such code: 试试这样的代码:

$somethings = array(); 
while ($row = mysql_fetch_assoc($result)) { 
    $somethings[] = $row;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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