我正在尝试验证我从SQL查询获得的结果,并检查是否返回零结果我正在尝试回显相应的消息。然而,我正在努力做到这一点,我试图使用如果声明,但没有工作。以下是我的代码。
<?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 :(得分: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 :(得分: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>";
}
答案 2 :(得分:-1)
相当简单,你会找到类似的东西......
if(mysql_affected_rows() >= 1)
{
// your while loop here
}
else
{
// processing for no returned results
}