<?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);
?>
Please help me, I couldn’t figure out as to why this code cannot return the result properly. It returned less 1 row (if result suppose to have 2 rows it will only return 1) from the exact rows available in the database.
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);
?>
if I just wrote this code, it works perfectly.
$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);
?>
But I want it to show “No data found” if the result was empty. So, any help? I know it’s very simple but I couldn’t find the answer for days already. Thank you in advance!
You are basically slicing off the first result in your if statement:
Just check for the number of rows returned to keep the resultset intact: