如果数据库中存在行返回行返回错误

时间:2017-06-10 07:43:11

标签: php mysql mysqli

<?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);

?>

这是我的PHP! 我的connect.php与数据库有连接。 一切正常,直到if语句,但如果行不存在,程序不执行else语句。

5 个答案:

答案 0 :(得分:2)

你应该在while循环之前检查条件(如果你没有行,那么循环就不会执行。)

  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.";
  }

答案 1 :(得分:2)

只需将代码替换为给定的代码:

<?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);

    ?>

实际上你在循环中使用if条件,这是错误的,我把它放在循环之前。

就是这样。 :)

答案 2 :(得分:0)

ifelse移出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);

?>

答案 3 :(得分:0)

如果没有更多行要提取,

mysql_fetch_array会返回NULL,因此如果没有行,则不会输入while循环。你应该把支票放在它外面,而不是在里面:

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.";
}

答案 4 :(得分:0)

您可以删除以下行

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

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

else{
   echo mysqli_error($mysqli);
}

你的$ row = mysqli_fetch_array($ comments)部分应该进入if($ mysql_num_rows&gt; 0)块

相关问题