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

时间:2015-03-12 17:44:38

标签: php mysql forms search

当我在一个只有大约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);

?>

2 个答案:

答案 0 :(得分:1)

你试过缓冲结果吗?

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

来自PHP Manual

  

mysqli_num_rows()的行为取决于是使用缓冲还是未缓冲的结果集。对于无缓冲的结果集,mysqli_num_rows()将不会返回正确的行数,直到检索到结果中的所有行。

答案 1 :(得分:0)

您的empty()功能可能会出现问题(取决于您的数据),请改用isset()

empty():如果变量为空字符串,false,array(),NULL,“0?,0和未设置变量”,则返回true。

isset():仅当变量不为空时才返回true。

修改

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

尝试这样的调试:在mysqli_query之前打印$ sql,在phpmyadmin中复制sql并检查查询结果并查询自己。