2013-10-24 66 views
0

我在這裏有一些php代碼,它應該顯示來自一個名爲'Collections'的字段的unique/disctinct值。我已成功連接到其他地方的數據庫。當此PHP代碼執行,瀏覽器給了我一個「警告:mysql_fetch_array():提供的參數不是在mypage.php一個有效的MySQL結果資源在第20行」Iss具有不同的值

  $id = isset($_GET['id'])?(int)$_GET['id']:0; // if $_GET['id'] exists, return it as an integer, otherwise use a sentinel, id's usually start with 1, so 0 works 

     if ($id!=0): 
// I assume this is a specific news item meaning you know it's ONE result 
$query = 'SELECT * DISTINCT (Collections) FROM Audios LIMIT 40'; // so try to use limit  1, no need to add extra steps in the database lookup 
    else: 
$query = 'SELECT * DISTINCT (Collections) FROM Audios ORDER BY Collections DESC LIMIT 40'; 
    endif; 

    $result = mysql_query($query); 
; 
// now loop through the results ***(This is Line 20)*** 
while ($row = mysql_fetch_array($result)){ 
// and use'em however you wish 
echo ' 

<div> 

    <li><a href="#">'.$row['Collections'].'</a></p> 
</div> 
'; 
    } 
+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo

回答

1
SELECT DISTINCT (Collections) FROM Audios ORDER BY Collections DESC LIMIT 40 

兩個取出*查詢。

+0

大聲笑謝謝,這工作....我想我需要從盯着機器休息。然後我再次懷疑我會自己修復它 – 011289User