<?php
include 'db.php';
$subassetcategory = $_GET["subassetcategory"];
if ($subassetcategory == "all") {
$sql = "SELECT * FROM asset_subasset";
}
else {
$sql = "SELECT * FROM asset_subasset WHERE subassetcategory = '" . $subassetcategory . "'";
}
$result = mysql_query($sql) or die(mysql_error());
if ($row = mysql_fetch_array($result)) {
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Subasset Category</th>
<th>Subasset Name</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
$subassetid = $row['subassetid'];
$assetid = $row['assetid'];
$subassetname = $row['subassetname'];
$subassetcategory = $row['subassetcategory'];
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $subassetcategory . "</td>";
echo "<td>" . $subassetname . "</td>";
echo "<td>" . "<a href='viewassetdetail.php?subassetid=$subassetid&assetid=$assetid'>View<a/>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "<br> No data found </br>";
}
mysql_close($connect);
?>
请帮帮我,我无法弄清楚为什么这段代码无法正常返回结果。它从数据库中可用的确切行返回少于1行(如果结果假设有2行,它只返回1)。
if ($row = mysql_fetch_array($result)) {
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Subasset Category</th>
<th>Subasset Name</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
$subassetid = $row['subassetid'];
$assetid = $row['assetid'];
$subassetname = $row['subassetname'];
$subassetcategory = $row['subassetcategory'];
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $subassetcategory . "</td>";
echo "<td>" . $subassetname . "</td>";
echo "<td>" . "<a href='viewassetdetail.php?subassetid=$subassetid&assetid=$assetid'>View<a/>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "<br> No data found </br>";
}
mysql_close($connect);
?>
如果我刚刚写了这段代码,那就完美了。
$result = mysql_query($sql) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Subasset Category</th>
<th>Subasset Name</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
$subassetid = $row['subassetid'];
$assetid = $row['assetid'];
$subassetname = $row['subassetname'];
$subassetcategory = $row['subassetcategory'];
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $subassetcategory . "</td>";
echo "<td>" . $subassetname . "</td>";
echo "<td>" . "<a href='viewassetdetail.php?subassetid=$subassetid&assetid=$assetid'>View<a/>" . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connect);
?>
但如果结果为空,我希望它显示“找不到数据”。那么,有什么帮助吗?我知道这很简单但我几天都找不到答案了。提前谢谢!
答案 0 :(得分:2)
你基本上是在if语句中切掉第一个结果:
if ($row = mysql_fetch_array($result))
只需检查返回的行数以保持结果集的完整性:
if(mysql_num_rows($result) > 0)
答案 1 :(得分:0)
您已使用mysql_fetch_array()
检查是否有任何结果。你已经在那里消耗了一排。所以,这就是你缺少一行的地方。您可以使用mysql_num_rows()
答案 2 :(得分:0)
If ($result->num_rows() > 0 ) { do your stuff } else { no results found }
答案 3 :(得分:0)
更简化的方法是在你进入循环数组之前检查结果集。这样你可以跳过所有这些,如果它是空的,直接进入空对话。
答案 4 :(得分:0)
而不是
if ($row = mysql_fetch_array($result))
{
.....................
}
尝试使用它:
$num_rows = mysql_num_rows($result); //returns the number of rows selected
if($num_rows==0)
{
//no data
}
else
{
//your code
}