开发者

Result returned less 1 (minus 1) from the exact row available in database

开发者 https://www.devze.com 2023-03-14 08:58 出处:网络
<?php include \'db.php\'; $subassetcategory = $_GET[\"subassetcategory\"]; if ($subassetcategory == \"all\") {
<?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'];
    $subassetcategor开发者_如何学编程y = $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:

if ($row = mysql_fetch_array($result))

Just check for the number of rows returned to keep the resultset intact:

if(mysql_num_rows($result) > 0)


You have use mysql_fetch_array() to check if there is any result. You have already consumed one row there. So, that is where your one missing row is. You can check if there is any value in the result by using mysql_num_rows()


If ($result->num_rows() > 0 ) { do your stuff } else { no results found }


A little more streamlined way of doing it would be to check the result set before you even get into looping the array. That way you can skip all that if its empty and go straight to the empty dialogue.


instead of

if ($row = mysql_fetch_array($result))
{
  .....................
}

try to use this:

 $num_rows = mysql_num_rows($result); //returns the number of rows selected

 if($num_rows==0)
 {
      //no data
 }
 else
 {
    //your code
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消