0

我创建了以下代码,但由于某种原因,它正在回显Array而不是结果:

<?php
    include("../config.php");
    include("functions.php");

    $count = "SELECT `monthly_slots` FROM users WHERE `username` = 'Bill'";
    $count = mysql_query($count);
    $data = mysql_fetch_assoc($count);
    echo "$data";
?>

有任何想法吗?

我不知道它为什么输出数组,因为该查询应该只有一个结果。

4

6 回答 6

2

试试这个

print_r($data);

这将输出数组的内容。

的返回类型mysql_fetch_assoc是数组。所以你应该使用print_r来查看结果。

Returns an associative array that corresponds to the fetched row and moves the internal data
pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC 
for the optional second parameter. It only returns an associative array.
于 2012-05-06T12:50:10.933 回答
2

mysql_fetch_assoc() 返回一个数组,您需要使用它print_r($data)来转储数组的内容。

于 2012-05-06T12:50:28.240 回答
1

正如array手册页所解释的,当您在字符串上下文中输出一个数组变量(echo如此处)时,它将变为 just "Array"

要查看数组内容,请使用print_rorvar_dump代替:

print_r($data);

或者更好的是,只需访问您想要的内容:

print($data["monthly_slots"]);
于 2012-05-06T12:51:34.180 回答
0

这是预期的。你应该试试print_r($data)

于 2012-05-06T12:50:25.060 回答
0

解决方案

  • 您可以简单地打印出数组的全部内容

    print_r($data)

但是,考虑到您只是在寻找特定的物品,我通常不建议您这样做。

  • 您也可以尝试将输出引用为第一项(假设只有一个结果)

    echo "$data[0]";.

  • 您也可以简单地引用您要查找的数组中的特定项目

    echo $data['monthly_slots'].

参考:

于 2012-05-06T12:53:36.673 回答
0

用于print_r($data)打印$data数组或$data['column_name']特定行。因为mysql_fetch_assoc()总是返回一个数组。

于 2012-05-06T12:54:07.517 回答