dongyuan2652 2013-12-23 21:07
浏览 51
已采纳

我的搜索引擎发出“mysql_fetch_array()期望参数1是资源,在select中给出布尔值”(关闭)[重复]

I have made a search engine, but every time I search for something like "Black Pants" it comes up with this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/u567454288/public_html/search/search.php on line 36 No results found for Black Pants

Here is the code:

<?php 
error_reporting(E_ALL ^ E_NOTICE); 
ini_set('display_errors', '1'); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<titleSearch For <?php echo $_GET['k']; ?></title>
</head>
<body>

    <h2>Search Engine</h2>
    <form action='./search.php' method='get'>
        <input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' required='required' />
        <input type='submit' value='Search' />
    </form>
    <hr />
    <?php 
        $k = $_GET['k'];
        $terms = explode(" ", $k);
        $query = "SELECT * FROM products WHERE ";

        foreach($terms as $each){
            $i++;
            if ($i == 1)
                $query .= "product_name LIKE '%$each%' ";
            else
                $query .= "product_name LIKE '%$each%' ";
        }

        // connect
        require("../storescripts/connect_to_mysql.php");

        $query = mysql_query($query);
        $numrows = mysql_num_rows($query);
        if ($numrows > 0){

            while($row = mysql_fetch_assoc($query)){
                $id = $row['id'];
                $product_name = $row['product_name'];
                $price = $row['price'];
                $details = $row['details'];
                $category = $row['category'];
                $subcategory = $row['subcategory'];
                $date_added = $row['date_added'];

                echo '<table width="100%" border="0" cellspacing="0" cellpadding="6">
                <tr>
                    <td width="17%" valign="top"><a href="../product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
                    <td width="83%" valign="top"><b>' . $product_name . '</b><br />
                        $' . $price . '<br />
                    <a href="../product.php?id=' . $id . '">View Product Details</a></td>
                </tr>
                </table>';
            }

        }
        else
            echo "No results found for <b>$k</b>";

        // disconnect
        mysql_close();

    ?>
</body>
</html>

Can you suggest how to fix the problem, so I can add a space within words and still be able to search?

</div>
  • 写回答

1条回答 默认 最新

  • doumao1917 2013-12-23 21:14
    关注

    Your code creates the following SQL for your black pants search:

    SELECT * FROM products WHERE product_name LIKE '%Black%'product_name LIKE '%Pants%' 
    

    Replace your original code block:

    $query = "SELECT * FROM products WHERE ";
    
    foreach($terms as $each){
        $i++;
        if ($i == 1)
            $query .= "product_name LIKE '%$each%' ";
        else
            $query .= "product_name LIKE '%$each%' ";
    }
    

    With this:

    $query = "SELECT * FROM products WHERE 1=1";
    
    foreach($terms as $each)
        $query .= " AND product_name LIKE '%$each%'";
    

    The AND above could be OR as well.

    This will result in an SQL statement:

    SELECT * FROM products WHERE 1=1 OR product_name LIKE '%Black%' OR product_name LIKE '%Pants%' 
    

    Pay attention to the comments on your OP as well as there area lot of best practice recommendations there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度