drx3157 2017-06-10 07:43
浏览 42
已采纳

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

<?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.

  • 写回答

5条回答 默认 最新

  • doutan1671 2017-06-10 07:47
    关注

    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.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog