简体   繁体   English

无法从MySQL输出json-获取空值

[英]Cannot output json from MySQL - getting null value

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~ :) 好快! 欢迎来到哟〜:)好快!

it would appear that $row['something'] is returning a null value for every row. 看来$row['something']为每行返回一个空值。 Make sure you've got proper data to output. 确保您有适当的数据要输出。

Do a var_dump($row) inside your loop to see what your database is returning. 在循环内执行var_dump($row)以查看数据库返回的内容。 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']); 尝试array_push($ somethings,$ row ['something']);

Also, it is better to use ajax to retrieve data, than to directly assigning to a string. 同样,使用ajax检索数据比直接分配给字符串更好。

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

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