检查mysql db中是否没有结果


Check if no result found in mysql db

我在mysqli中编写了一个php搜索表,它工作得很好,但我想向用户显示正确的消息,如果没有发现结果。

下面是我当前的代码:

$search_string=$_GET["design"];
$connect= mysqli_connect("mysql.myhost.com","abc","123456","mydb_db");
$query="select * from product where product_design like '%$search_string%'";
$rows= @mysqli_query($connect,$query) or die("Error: ".mysqli_error($connect));
if ($rows!=null)//I put this if to check if there is any result or not but its not working
{
while(($record=mysqli_fetch_row($rows))!=null)
{
    .
            .//i have working code for showing the result here
            .
}   
mysqli_close($connect);
}
else{
echo"no result found";
}

你能帮我什么是错的,即使我搜索的东西是不存在的db仍然程序不显示"没有结果发现"

谢谢

您需要的是mysqli_num_rows,特别是mysqli_result::num_rows位。这将为mysqli结果集添加num_rows属性。这意味着您可以执行

$rowCount = $rows->num_rows

也有一个非oo等效…

$rowCount = mysqli_num_rows($rows);

(区别纯粹是编码风格的不同)

使用其中一个来确定返回多少条记录并输出适当的消息。

下面这行没有意义,这可能是问题所在。

while(($record=mysqli_fetch_row($rows))!=null)

然而,如果$row是空的,它不会返回'null',但没有设置。这样做:

if ($rows) { echo 'works';} else { echo 'no rows'; }