微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 警告:mysqli_num_rows()期望参数1为mysqli_result,[复制]中给出布尔值

参见英文答案 > PHP & MySQL: mysqli_num_rows() expects parameter 1 to be mysqli_result,boolean given 2个
挣扎着我的网页设计任务.我一直在按照教程添加我的网站的搜索功能,但我一直收到以下错误

警告: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’不存在”

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

无论如何,感谢你的帮助到目前为止,我为我的完全无知而道歉.

我一直在努力教自己,但不幸的是,时间是我现在所没有的奢侈品.

问题是您的查询返回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吗?也许它真的像用户或其他东西.

原文地址:https://www.jb51.cc/php/135796.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐