PHP/MYSQL 多搜索表单不显示包含大型数据库内容的记录

PHP/MYSQL Multiple Search Form not showing records with large database content

当我在一个只有大约 10 条记录的数据库中查询时,我的搜索表单可以正常工作,但是当我查询超过 10 条记录时,它不会显示任何记录并转到我的其他“0 条记录”请帮忙,下面是我的 php 代码。

    <?php

    $sfname = $_POST["fname"];
    $sgen = $_POST["gen"];
    $sdoc = $_POST["doc"];
    $smisc = $_POST["misc"];
    $ssick = $_POST["sick"];
    $sothers = $_POST["others"];
    $servername = "localhost";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxx";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    if(!empty($sfname) || !empty($sgen) || !empty($sdoc) || !empty($smisc) || !empty($ssick) || !empty($sothers) ){

            $genQueryPart = !empty($sgen) ? "Gender LIKE '%$sgen%'" : "";
            $fnameQueryPart = !empty($sfname) ? "FullName LIKE '%$sfname%'" : "";
            $docQueryPart = !empty($sdoc) ? "Doctor LIKE '%$sdoc%'" : "";
            $miscQueryPart = !empty($smisc) ? "Misc LIKE '%$smisc%'" : "";
           $sickQueryPart = !empty($ssick) ? "Sickness LIKE '%$ssick%'" : "";
            $othersQueryPart = !empty($sothers) ? "Others LIKE '%$sothers%'" : "";

            $arr = array($genQueryPart, $fnameQueryPart,$docQueryPart,$miscQueryPart,$sickQueryPart,$othersQueryPart);

            $sql = "select * from index where";

            $needsAnd = false;
        for ($i = 0; $i < count($arr); $i++) {
            if ($arr[$i] != "") {
                if ($needsAnd) {
                    $sql .= " AND ";
                }

                $needsAnd = true;
                $sql .= " " . $arr[$i];
            }

        }

    //Get query on the database
        $result = mysqli_query($conn, $sql);
    //Check results
        if (mysqli_num_rows($result) > 0)
        {
        //Headers
          echo "<table border='1' style='width:100%'>";
            echo "<tr>";
             echo "<th>File ID</th>";
            echo "<th>Full Name</th>";
            echo "<th>Gender</th>";
            echo "<th>Doctor</th>";
            echo "<th>Misc</th>";
            echo "<th>Sickness</th>";
            echo "<th>Others</th>";
            echo "</tr>";

      //output data of each row
          while($row = mysqli_fetch_assoc($result))
            {
                    echo "<tr>";
                      echo "<td>".$row['FileID']."</td>";
                      echo "<td>".$row['FullName']."</td>";
                      echo "<td>".$row['Gender']."</td>";
                      echo "<td>".$row['Doctor']."</td>";
                      echo "<td>".$row['Misc']."</td>";
                    echo "<td>".$row['Sickness']."</td>";
    echo "<td>".$row['Others']."</td>";
                    echo "</tr>";

            }
                  echo "</table>";
        }
     else {
            echo "0 results";
        }

    } else {
        echo "You must enter at least one value";
    }
mysqli_close($conn);

?>

您尝试过缓冲结果吗?

$result = mysqli_query($conn, $sql);
mysqli_store_result($conn);
if(mysqli_num_rows($result) > 0) {
 ...
}

来自 PHP Manual

The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.

您的 empty() 函数可能有问题(取决于您的数据),请改用 isset()

empty() : 如果变量是空字符串,则 return 为真,false,array(),NULL,“0?,0,和未设置的变量。

isset() :仅当变量不为空时 return 才为真。

编辑

用户从我下面的评论中得到了答案

尝试这样调试:在[=30=之前打印$sql,在phpmyadmin中复制sql并检查查询结果并自行查询。