php 警告:mysqli_num_rows() 期望参数 1 是 mysqli_result,布尔值在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16756596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:39:27  来源:igfitidea点击:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in

phpmysql

提问by Chrissy

struggling with my web design assignment. I've been following a tutorial to add in a search feature for my website, but I've been getting the following error:

努力完成我的网页设计任务。我一直在学习为我的网站添加搜索功能的教程,但出现以下错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /search.php on line 31

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

line 31 is (or was)

第 31 行是(或曾经)

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

That was the original error. as per instructions in the comments, I've since revised the code:

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

<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>

made necessary changes and updated yet again -

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

now the only error message I'm getting here is "Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist"

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

I'm assuming that I need to change the username, perhaps because it's too similar?

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

Anywho, thanks for your help so far, and I apologise for my complete ignorance.

Anywho,感谢您到目前为止的帮助,我为我的完全无知道歉。

I've been trying to teach myself, but unfortunately time is a luxury I just don't have at the moment.

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

回答by chrislondon

The problem is your query returned falsemeaning there was an error in your query. After your query you could do the following:

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

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

Or you could combine it with your query:

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

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

That will print out your error.

这将打印出您的错误。

Also... you need to sanitize your input. You can't just take user input and put that into a query. Try this:

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

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

In reply to: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist

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

Are you sure the table name is sookehhh_shopsy_db? maybe it's really like users or something.

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