缺少从数据库打印的结果

时间:2015-06-01 03:47:13

标签: php mysql

我的目的是从输入键盘搜索书类型并计算它。我有错误:计算书的数量是正确的但是在打印时,它会遗漏一些结果。例如,我把'f'显示7结果(右)但是它只打印2个结果。这是我的代码:

<form action="" method="POST">
        Enter type of book here:<input type="text" size="20" name="sbt"> <br />
        <input type="submit" name="sb" value="Search">
    </form>
    <table align="center" border="1" width="600">
        <thead><tr align="center">
    <tr align="center">
            <td><b>Book ID</b></td>
            <td><b>Book Title</b></td>
            <td><b>Book Author</b></td>
            <td><b>Pulished Year</b></td>
            <td><b>Book Type</b></td>
            <td><b>Status</b></td>
        </tr>
        <?php
        if (isset($_POST['sb'])) {
            $s="";
            if ($_POST['sbt'] == null) {
                  echo "Please re-enter <br>";
                } else
                {
                  $s = $_POST['sbt'];
                }
            $q = "SELECT * FROM book WHERE book_type LIKE '%$s%' ";
            $r= mysqli_query($conn,$q);
            while($row = mysqli_fetch_array($r)) {
             ?>
        <tr align="center">
                <td><?php echo $row['book_no'];?></td>
                <td><?php echo $row['book_title'];?></td>
                <td><?php echo $row['book_author'];?></td>
                <td><?php echo $row['book_year'];?></td>
                <td><?php echo $row['book_type'];?></td>
                <td>
                <?php 
                if ($row['book_quantity'] == 1) {
                                echo "Available";
                            }
                else {
                                echo "Not available"; 
                            }  

                ?>
                 </td>

            <?php
             $c=" SELECT COUNT(DISTINCT(book_no)) AS totaltype FROM book WHERE book_type LIKE '%$s%'";
             $s= mysqli_query($conn, $c);
             if (mysqli_num_rows($s)> 0 )
             {
                $row2= mysqli_fetch_array($s);
                echo " Total book type result:"."{$row2['totaltype']}";
            }
        } 

        ?>
         <?php } ?>
    </table>

1 个答案:

答案 0 :(得分:0)

"%"之前移除$s并尝试。

例如:

$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '$yourPHPVAR%'");