无法解决“警告:mysqli_num_rows()期望参数1为mysqli_result”

时间:2018-08-19 20:03:47

标签: php mysql wampserver

当前有一个搜索框设置,用于获取变量$txtsearch以获取animaltypeanimalspecies,但是查询返回错误“警告:mysqli_num_rows()期望参数1为mysqli_result “如何更正查询?

if ($txtsearch == "") {
            echo "Please a enter search criteria!Please try again <br/>"; 
        }else{
        if($lstsearch == 'animaltype'){

        $query = "SELECT COUNT(orders.orderid),orders.animalid,catalogue.animalname,catalogue.animaltype
                    from catalogue,orders
                    where catalogue.animalid = orders.animalid 
                    and catalogue.animaltype LIKE %$txtsearch%'
                    group by catalogue.animaltype";

                $result = mysqli_query($db, $query);
                $num = mysqli_num_rows($result);
                if ($num == 0){
                    echo "Type " . $txtsearch . " was not found!
                    <br/> " . "<a href = 'OrderNoOfTypeSpeciesQuery.php'> Return </a> to Order Query Retrieval search!";

                }else{
                    ?>

                    <table>
                    <tr>
                        <th>Number Of Orders</th>
                        <th>Animal ID   </th>
                        <th>Animal Name </th>
                        <th>Animal Type </th>
                    </tr>

                    <?php
                while ($rows = mysqli_fetch_array($result))
                        {
                            ?>
                    <tr>
                         <td> <?php echo $rows[0]; ?>  </td>
                         <td> <?php echo $rows[1]; ?> </td>
                         <td> <?php echo $rows[2]; ?>  </td>
                         <td> <?php echo $rows[3]; ?> </td>


                    </tr>

                        <?php
                        }
                        ?>
                           <a href ="OrderNoOfTypeSpeciesQuery.php">Return to Order Query Retrieval page</a>
                        </table?
                        <?php
                } 
            }

1 个答案:

答案 0 :(得分:-1)

抱歉,我很傻,我忘了在'txtsearch'上添加'

因此答案是

    $query = "SELECT COUNT(orders.orderid),orders.animalid,catalogue.animalname,catalogue.animaltype
                from catalogue,orders
                where catalogue.animalid = orders.animalid 
                and catalogue.animaltype LIKE '%$txtsearch%'
                group by catalogue.animaltype";