如果数据库中存在行返回行返回错误[关闭]

<?php 
include 'connect.php';
    $stfind = $_GET['startup_find'];
    $sqlselect = "SELECT * from startup_schema where Name = '$stfind'";
    $comments = mysqli_query($mysqli, $sqlselect);
    $mysql_num_rows = mysqli_num_rows($comments);

    while ($row = mysqli_fetch_array($comments)) {
        if($mysql_num_rows > 0) {
            echo "<table border='1' style='width: 100%'>";  
            echo "<tr>"; // LINE ADDED
            echo "<th>ID</th>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<th>Name</th>";
            echo "<td>" . $row['Name'] ."</td>";
            echo "</tr></table>";
        } else {
            echo "No Data Found.";
            }
        }

mysqli_close($mysqli);

?>

Here is my PHP! my connect.php has connection to database. Everything is working fine till if statement but the if the rows does not exist the program is not executing the else statement.

mysql_fetch_array returns NULL if there are no more rows to fetch, so the while loop is not entered if there are no rows. You should place your check outside it, not inside it:

if($mysql_num_rows > 0) {
    while ($row = mysqli_fetch_array($comments)) {        
        echo "<table border='1' style='width: 100%'>";  
        echo "<tr>"; // LINE ADDED
        echo "<th>ID</th>";
        echo "<td>" . $row['ID'] . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<th>Name</th>";
        echo "<td>" . $row['Name'] ."</td>";
        echo "</tr></table>";
    } 
} else {
    echo "No Data Found.";
}

you should check the condition before the while loop (if you have no rows then the loop will not execute.)

  if($mysql_num_rows > 0) {
        while ($row = mysqli_fetch_array($comments)) {
          echo "<table border='1' style='width: 100%'>";  
          echo "<tr>"; // LINE ADDED
          echo "<th>ID</th>";
          echo "<td>" . $row['ID'] . "</td>";
          echo "</tr>";
          echo "<tr>";
          echo "<th>Name</th>";
          echo "<td>" . $row['Name'] ."</td>";
          echo "</tr></table>";
      }

  }  else {
    echo "No Data Found.";
  }

Just replace your code with the given one:

<?php 
    include 'connect.php';
        $stfind = $_GET['startup_find'];
        $sqlselect = "SELECT * from `startup_schema` where `Name` = '".$stfind."'";
        $comments = mysqli_query($mysqli, $sqlselect);
        $mysql_num_rows = mysqli_num_rows($comments);

        if($mysql_num_rows > 0) {
            while ($row = mysqli_fetch_array($comments)) {

                echo "<table border='1' style='width: 100%'>";  
                echo "<tr>"; // LINE ADDED
                echo "<th>ID</th>";
                echo "<td>" . $row['ID'] . "</td>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Name</th>";
                echo "<td>" . $row['Name'] ."</td>";
                echo "</tr></table>";

            }
        } 
        else {
                echo "No Data Found.";
        }

    mysqli_close($mysqli);

    ?>

Actually you were using if condition inside while loop, which is wrong, i put it before while loop.

That's it. :)

Move if and else out of while:

<?php 
    include 'connect.php';
    $stfind = $_GET['startup_find'];
    $sqlselect = "SELECT * from startup_schema where Name = '$stfind'";
    $comments = mysqli_query($mysqli, $sqlselect);
    $mysql_num_rows = mysqli_num_rows($comments);

    if($mysql_num_rows > 0) {
        while ($row = mysqli_fetch_array($comments)) {
            echo "<table border='1' style='width: 100%'>";  
            echo "<tr>"; // LINE ADDED
            echo "<th>ID</th>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<th>Name</th>";
            echo "<td>" . $row['Name'] ."</td>";
            echo "</tr></table>";
        }
    } else {
        echo "No Data Found.";
    }

    mysqli_close($mysqli);

?>

You can subtitute the following line

$comments = mysqli_query($mysqli, $sqlselect);

with

if($comments = mysqli_query($mysqli, $sqlselect)){
   // do your tasks here
}

else{
   echo mysqli_error($mysqli);
}

And you the $row = mysqli_fetch_array($comments) part should come inside the if($mysql_num_rows > 0) block