7

为我的网页设计任务而苦苦挣扎。我一直在按照教程为我的网站添加搜索功能,但出现以下错误:

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在第 31 行的 /search.php 中给出布尔值

第 31 行是(或曾经)

<pre>if(mysqli_num_rows($results) >= 1)</pre>

那是最初的错误。根据评论中的说明,我已经修改了代码:

<pre>



    <?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyword']);

//check whether the name parsed is empty
if($searchTerm == "")
{
    echo "Enter the name/brand of what you're looking for.";
    exit();
}

//database connection info
$host = "localhost";
$db_name = "sookehhh_shopsy_db";
$username = "sookehhh_shopsy";
$password = "xxxx";



//connecting to server and creating link to database
$link = mysqli_connect($host, $username, $password, $db_name) or die('Could not connect: ' . mysqli_connect_error());

//MYSQL search statement
$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm)  . "%'";

// original query$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

//added suggestion below - not sure if correct place?
if (!$result) {
    die(mysqli_error($link)); 
}

/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
    {
        $output .= "Product Name: " . $row['name'] . "<br />";
        $output .= "Price: " . $row['price'] . "<br />";
    }
    echo $output;
}
else
    echo "There was no matching record for that item " . $searchTerm;
?>
</pre>

进行了必要的更改并再次更新 -

现在我在这里得到的唯一错误消息是“表'sookehhh_shopsy_db.sookehhh_shopsy_db'不存在”

我假设我需要更改用户名,也许是因为它太相似了?

任何人,感谢您迄今为止的帮助,我为我的完全无知道歉。

我一直在努力自学,但不幸的是,时间是我目前没有的奢侈品。

4

1 回答 1

17

问题是您的查询返回false意味着您的查询中有错误。查询后,您可以执行以下操作:

if (!$result) {
    die(mysqli_error($link));
}

或者你可以将它与你的查询结合起来:

$results = mysqli_query($link, $query) or die(mysqli_error($link));

这将打印出您的错误。

另外......你需要清理你的输入。您不能只接受用户输入并将其放入查询中。尝试这个:

$query = "SELECT * FROM shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";

回复:表 'sookehhh_shopsy_db.sookehhh_shopsy_db' 不存在

你确定表名是sookehhh_shopsy_db 吗?也许它真的像用户什么的。

于 2013-05-26T05:56:28.143 回答