PHP查询错误

发布于 2024-11-03 07:32:42 字数 459 浏览 3 评论 0 原文

我使用 LIKE 进行搜索,我在 phpMyAdmin 中尝试并返回结果,但是当我在 php 中使用它时,它返回空结果。

$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
    $nrows = 0;
else
    $nrows = mysql_num_rows($result);

当我使用 phpMyAdmin 运行此查询时,它将返回结果,但当我在 php 中使用它时,它返回空。

更新:

抱歉,大家,

我刚刚发现问题是我也没有连接数据库。不管怎样,谢谢你的帮助。

I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.

$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
    $nrows = 0;
else
    $nrows = mysql_num_rows($result);

It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.

Update:

Sorry guys,

I just found out the problem is i didn't connect database as well. anyway, thanks for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一绘本一梦想 2024-11-10 07:32:42

试试这个

 $query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";

Try This

 $query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
攒眉千度 2024-11-10 07:32:42

如果代码的唯一目的是获取具有搜索名称的产品数量,请使用 SELECT COUNT(*) 而不是执行 mysql_num_rows()您的所有数据。它将减少您的查询时间和(不必要的)获取的数据量。

And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.

余生共白头 2024-11-10 07:32:42

我不确定为什么这不起作用,因为查询对我来说似乎是正确的。我想建议你这样写查询

$query = <<

 SELECT * FROM 产品,其中产品名称 LIKE "%$search%" LIMIT $start,30

SQL;

请注意,SQL; 后面不应有任何空格或任何字符

I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way

$query = <<<SQL

 SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30

SQL;

please note that there should not be any space or any character after SQL;

复古式 2024-11-10 07:32:42
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
扛起拖把扫天下 2024-11-10 07:32:42

您可以直接使用 mysql_num_rows()

但这里是正确的代码

$query = "SELECT * FROM Product WHERE Product_name LIKE '%".$search."%' LIMIT $start,30";

you can use directly mysql_num_rows()

but here is right code

$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";

离线来电— 2024-11-10 07:32:42
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
    $nrows = 0;
} else{
    $nrows = mysql_num_rows($result);
}

//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
    $nrows = 0;
} else{
    $nrows = mysql_num_rows($result);
}

//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文