2012-03-20 31 views
0

如果我的查詢沒有返回任何內容,如何顯示消息? 我嘗試這樣做:如果查詢沒有返回,則顯示消息

while($info2 = mysql_fetch_assoc($data2)) 
{ 
    // la la la lots of code here 
} 
else 
{ 
    echo "Nothing Returned"; 
} 

只有得到這個錯誤:

Parse error: syntax error, unexpected T_ELSE

感謝您的幫助大家!

+4

請閱讀[手冊](http://php.net/manual/en/control-structures.while.php)。在任何編程語言中都沒有'while' /'else'語法。像這樣的問題只是令人尷尬。 – Jon 2012-03-20 22:24:59

+0

我通過在MySQL論壇上的谷歌搜索找到了這個。 – pufAmuf 2012-03-20 22:27:44

回答

1

喜歡的東西:

if(mysql_num_rows($data2) > 0){ 
    //while loop goes here 
} else { 
    //echo message 
} 
+0

沒關係,它的工作原理! – pufAmuf 2012-03-20 22:41:20

1

使用mysql_num_rows檢查由查詢返回

if(mysql_num_rows($data2) > 0) 
{ 
    while($info2 = mysql_fetch_assoc($data2)) 
    { 
    ///la la la lot's of code here 
    } 
} 
else 
{ 
echo "Nothing Returned"; 
} 
+0

非常感謝Teez! – pufAmuf 2012-03-20 22:42:50

1

一個簡單的PHP函數的行數:

if (mysql_num_rows($data2) != 0) 
{ 
    // your while 
} 
else 
{ 
    // if nothing 
} 

此功能只算多少行返回。

+0

認爲你有你的意見逆轉... :-) – keithhatfield 2012-03-20 22:26:54

+0

@dleiftah在清楚? – 2012-03-20 22:29:16

+0

你顯示while循環在'num_rows == 0'的地方出現,實際上,錯誤信息應該出現在'num_rows == 0'的地方...... – keithhatfield 2012-03-20 22:30:43