在PHP/MySQL中,我是一個很不錯的選擇,但是,現在我需要從兩個相關的表中獲取信息。 我對我需要做的事情做了很多研究,但似乎無法弄清楚我出錯的地方。我的問題出在哪裏?
問題:如何從短鏈接表中獲取與shortlink_analytics中的短鏈接有關的鏈接?
我得到的錯誤:警告:mysql_fetch_array()預計參數1是資源,布爾給
我的表如下:
Shortlinks表
ID
shortlink
鏈接
createdTime
createdBy
shortlink_analytics表
shortlink
引用
hitTime
USERIP
的userAgent
到目前爲止,我已經嘗試的代碼是:
$linecount = 1;
//$mostPop is where I think I have gone wrong and this is where the warning refers to
$mostPop = "SELECT shortlink, COUNT(shortlink) FROM shortlink_analytics JOIN shortlinks ON shortlink_analytics.shortlink = shortlinks.shortlink GROUP BY shortlink ORDER BY COUNT(shortlink) DESC LIMIT 10";
$loadPop = mysql_query($mostPop);
echo '<table id="middleIndex">';
echo '<tr><td class = "overFlow"><h2>Most Popular Shortlink</h2></td></tr>';
echo '<tr>';
while($row = mysql_fetch_array($loadPop))
{
echo '<td class = "overFlow">'.$row[1].' visits - <a href = "info.php?link='. $row['shortlink'] .'">hud.ac/' . $row['shortlink'] . '</a></td>';
echo '</tr>';
$linecount++;
}
echo '<tr id="indexMiddle"><td id="hand" class = "overFlow"><a onclick="indexMostPopular()">View More</a></td></tr>';
echo '</table>';
我beleive,我沒有背後如何使用JOIN或者是參數的透徹理解不夠需要形成一個成功的加入。
使用'mysql_error'找出SQL錯誤是從那裏工作。但請注意,mysql_ *函數已被棄用,如果可能的話不應該被真正使用。您應該考慮使用mysqli或PDO。 –
只是幾個筆記。 a)使用mysql_ *系列函數非常不鼓勵,因爲它們不再被真正維護,並且除了名字之外都被棄用。 YOu可能想改爲使用PDO或mysqli。b)不建議混合顯示邏輯(回顯HTML標記)和應用程序邏輯(數據庫查詢等),因爲它可能導致代碼難以維護。 – GordonM
歡呼傢伙,我明白mysql_ *現在已經貶值了,我正在將我的知識轉移到一個新的約定,感謝您的反饋! –