检查在mysql db中是否找不到结果 [英] Check if no result found in mysql db

查看:72
本文介绍了检查在mysql db中是否找不到结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个phpi在mysqli中搜索一个表,它工作正常,但是如果没有找到结果,我想向用户显示正确的消息.

I wrote a php search for a table in mysqli and it works fine but i want to show the correct message to user if no result were found.

这是我当前的代码:

$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";
}

您能帮我解决什么问题吗,即使我搜索数据库中不存在的内容,程序仍不会显示未找到结果"

could you please help me what is wrong , even when i search for something which is not exist in the db still the program not displaying "no result found"

谢谢

推荐答案

您需要的是 mysqli_num_rows ,特别是mysqli_result::num_rows位.这会将num_rows属性添加到mysqli结果集中.这意味着您可以

What you need is mysqli_num_rows specifically the mysqli_result::num_rows bit. This adds a num_rows property to mysqli result sets. This means you can do

$rowCount = $rows->num_rows

还有一个非OO等效项...

There's also a non-OO equivalent ...

$rowCount = mysqli_num_rows($rows);

(区别纯粹是编码风格之一)

(The difference is purely one of coding style)

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

Use one of these to determine how many records are returned and output the appropriate messages.

这篇关于检查在mysql db中是否找不到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆