dthyxna3894 2011-05-16 19:38
浏览 107
已采纳

我的PHP数组的第一个值在有值时返回为'null'

I'd really like some help with the following MySQL / PHP problem (maybe bug?)

I'm trying to retrieve and display an array of data from my database using MySQL / PHP, but when I echo out the array, it returns the first value as 'null'.

So, even though the database has the following info:

"Example 1", "Example 2", "Example 3"...

The php echos out:

"null", "Example 2, "Example 3"

I would imagine this would be a common problem, but I haven't managed to find the required information elsewhere on the internet, so I'm hoping you kind folks can help.

My PHP

/* If connection to database, run sql statement. */
if ($conn) {
    $fetch = mysql_query("SELECT column FROM table WHERE approved = '1' ");

    // declare empty array to fill later
    $result = array();

    // make sure the MySQL pointer is looking at the first row
    mysql_data_seek($fetch, 0);


    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

        foreach ($row as $value) {

            $row_array = $value;

            // push info into new array with just the value
            array_push($result, $row_array);
        }
    }
}
/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($result);

UPDATE

New code courtesy of Mark B:

if ($conn)
{
$sql = "SELECT column_name FROM table WHERE comment_approved = '1' "; 
$query = mysql_query($sql) or die(mysql_error());

$result = array();
while ($row = mysql_fetch_assoc($query)) {
    $result[] = $row['column_name'];

}

}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($result);

NOTE:

The 'null' problem still occurs with or without the:

 mysql_data_seek($fetch, 0);

as that appears to do nothing.

Any help would be great!

SOLVED

Thanks to Mark B who pointed out that the problem was probably in the database rather than the PHP, it turned out there was the character ` lurking where there should have been a '. This caused the information to appear 'null'.

  • 写回答

3条回答 默认 最新

  • douliexu5623 2011-05-16 19:45
    关注
    $sql = "SELECT column FROM table WHERE approved = '1'";
    $result = mysql_query($sql) or die(mysql_error());
    
    $data = array();
    while($row = mysql_fetch_assoc($result)) {
        $data[] = $row['column'];
    }
    
    echo json_encode($data);
    

    You shouldn't have to do the seek, as you've not done anything with the result at the time. And since you're only fetching a single column from the database, there's no need for the inner foreach() loop either.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题