我正在嘗試爲我的網站製作搜索腳本。目前爲止這一切都進展順利,但如果搜索結果不存在,我想顯示「沒有顯示結果」。我試過這個:如果數組爲空,PHP將顯示「無結果」
<?php
while($resultsarray(mysql_fetch_assoc($searchresult))//$searchresult is the result of my MySQL query
{
if($_GET['options']=='user') {
echo "<a href = 'userinfo.php?id=".$resultsarray['usernum']."'>".$resultsarray['username']."</a>";
}
else if($_GET['options']=='topics') {
echo "<a href = 'display_post.php?id=".$resultsarray['id']."'>".$resultsarray['subject']."</a>";
echo "<p>".$resultsarray['content']."</p>";
}
}
if(empty($resultsarray)) {
echo "<p>There are no results to display.</p>";
}
但是總是顯示消息,即使有結果。我也試過這個:
<?php
$resultsarray = mysql_fetch_assoc($searchresult);
if(empty($resultsarray)) {
echo "<p>There are no results to display.</p>";
}
while($resultsarray = mysql_fetch_assoc($searchresult))//$searchresult is the result of my MySQL query
{
if($_GET['options']=='user') {
echo "<a href = 'userinfo.php?id=".$resultsarray['usernum']."'>".$resultsarray['username']."</a>";
}
else if($_GET['options']=='topics') {
echo "<a href = 'display_post.php?id=".$resultsarray['id']."'>".$resultsarray['subject']."</a>";
echo "<p>".$resultsarray['content']."</p>";
}
}
但是那也沒用。任何有關這個問題的幫助表示讚賞。
做一個'var_dump($ resultsarray);' – GriffLab