2012-05-06 102 views
0

我創建了下面的代碼,但由於某種原因,它是呼應陣列,而不是結果:PHP顯示錯誤

<?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"; 
?> 

任何想法?

我不知道它爲什麼輸出數組,因爲應該只有一個來自該查詢的結果。

回答

2

mysql_fetch_assoc()返回一個數組,你需要使用print_r($data)來代替數組的內容。

+0

此外,您可能需要在print_r後回顯「

" before and "
」,以在瀏覽器中對其進行很好的格式化 – K2xL

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. 
0

它是預期的。你應該試試print_r($data)

1

正如手冊頁array所解釋的那樣,當你在字符串上下文中輸出一個數組變量時(如echo在這裏所做的),它將變成只是"Array"

要查看數組內容使用print_rvar_dump代替:

print_r($data); 

或者更好的只是訪問內容你想:

print($data["monthly_slots"]); 
0

解決方案

  • 你可以簡單地打印出來的陣列的全部內容與

    print_r($data)

但是我通常不會建議做這樣的考慮你只想找洙特定項目..

  • 您也可以嘗試將參考輸出作爲第一項(假設只有一個結果)與

    echo "$data[0]";

  • 你也可以簡單地引用數組中的特定項目,你正在尋找

    echo $data['monthly_slots'].

參考文獻:

0

使用print_r($data)打印$data陣列,或$data['column_name']用於perticular行。因爲mysql_fetch_assoc()往返返回一個數組。