2013-05-15 26 views
-1

我想驗證我的結果我從SQL查詢中獲得並檢查是否返回零結果我正在嘗試回顯相應的消息。然而,我正在努力做到這一點,我試圖使用if語句,但沒有工作的任何想法。以下是我的代碼。驗證來自mysql數據庫的結果

<?php 
$mysql_query = "SELECT ISO_id, country_name, population, gdp, gold, silver, bronze, total FROM Country WHERE ISO_id='$countryID'"; 
$runmysql_query=mysql_query($mysql_query); 

echo "<table class = 'table' border='4' align = 'center'> 
<tr class = 'title'> 
<th>ISOid</th> 
<th>Country Name</th> 
<th>Population</th> 
<th>GDP</th> 
<th>Gold</th> 
<th>Silver</th> 
<th>Bronze</th> 
<th>Total</th> 
</tr>"; //title headings declared 

while($row = mysql_fetch_array($runmysql_query)) 
{ 
    echo "<tr>"; 
    echo "<td class = 'results'>" . $row['ISO_id'] . "</td>"; 
    echo "<td class = 'results'>" . $row['country_name'] . "</td>"; 
    echo "<td class = 'results'>" . $row['population'] . "</td>"; 
    echo "<td class = 'results'>" . $row['gdp'] . "</td>"; 
    echo "<td class = 'results'>" . $row['gold'] . "</td>"; 
    echo "<td class = 'results'>" . $row['silver'] . "</td>"; 
    echo "<td class = 'results'>" . $row['bronze'] . "</td>"; 
    echo "<td class = 'results'>" . $row['total'] . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
?> 
+0

你湊ld使用mysql的num rows函數。然後,如果返回0行,則打印該消息,否則打印數據。 http://php.net/manual/en/function.mysql-num-rows.php但mysql已棄用,因此您應該使用mysqli – sunrize920

+2

請不要使用mysql_ *函數 - 它們已被棄用! – user4035

回答

0

試試這個:

<?php 
$mysql_query = "SELECT ISO_id, country_name, population, gdp, gold, silver, bronze, total FROM Country WHERE ISO_id='$countryID'"; 
$runmysql_query=mysql_query($mysql_query); 

$num_rows = mysql_num_rows($runmysql_query); 
if($num_rows > 0) 
{ 
//print table with rows 
} 
else 
{ 
    print "no data"; 
} 
-1

相當簡單,你要尋找的沿此線的東西...

if(mysql_affected_rows() >= 1) 
{ 
    // your while loop here 
} 
else 
{ 
    // processing for no returned results 
} 
+0

'mysql_affected_rows'用於刪除,插入,更新或替換。 'mysql_num_rows'用於選擇語句。有關更多信息,請參見[documentation](http://php.net/manual/en/function.mysql-query.php)。 – EmmyS

+0

看來mysql_ *函數現在已經被折舊了。 – Adam92

+0

這不完全是新的。他們已經被棄用了好幾年了。 – EmmyS

0

你可以使用while循環的醜陋一步姐姐do...while

if ($row = mysql_fetch_array($runmysql_query)){ 
    echo "<table>"; 
    do { 
     echo "<tr>"; 
     echo "<td class = 'results'>" . $row['ISO_id'] . "</td>"; 
     echo "<td class = 'results'>" . $row['country_name'] . "</td>"; 
     echo "<td class = 'results'>" . $row['population'] . "</td>"; 
     echo "<td class = 'results'>" . $row['gdp'] . "</td>"; 
     echo "<td class = 'results'>" . $row['gold'] . "</td>"; 
     echo "<td class = 'results'>" . $row['silver'] . "</td>"; 
     echo "<td class = 'results'>" . $row['bronze'] . "</td>"; 
     echo "<td class = 'results'>" . $row['total'] . "</td>"; 
     echo "</tr>"; 
    } while ($row = mysql_fetch_array($runmysql_query)); 
    echo "</table>"; 
} else { 
    echo "<p>Nothing to see Here</p>"; 
}