-1

我正在尝试验证从 SQL 查询中获得的结果,并检查是否返回零结果,我正在尝试回显适当的消息。然而,我正在努力做到这一点,我试图使用 if 语句但没有工作的任何想法。下面是我的代码。

<?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>";
?>
4

3 回答 3

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";
}
于 2013-05-15T20:58:48.753 回答
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>";
}
于 2013-05-15T21:09:36.480 回答
-1

相当简单,你会寻找类似的东西......

if(mysql_affected_rows() >= 1)
{
    // your while loop here
}
else
{
    // processing for no returned results
}
于 2013-05-15T20:56:02.217 回答