简体   繁体   English

如何从其中`cat` ='$ cat'的表中选择结果,如果不是,那么无论$ cat是什么,都从`cat`中全选

[英]How to select results from table where `cat` = '$cat' and if it's not then select all from `cat` no matter what $cat is

How to select results from table where cat = '$cat' and if it's not equal then select all from cat no matter what $cat is. 如何从cat ='$ cat'的表中选择结果,如果不相等,则无论$ cat是什么,都从cat全选。 Here is my query 这是我的查询

$sql = mysql_query("SELECT * FROM `obiavi` WHERE `postdate`+`days`*86400 > $time AND `town` IN ('$oblasti') AND `desc` LIKE '%$keyword%' ORDER BY id DESC LIMIT $start, $perpage") or die (mysql_error());

From my understanding, you want only results where cat = '$cat'. 据我了解,您只需要cat ='$ cat'的结果。 If you obtain no results, then you want to have results no matter what their cat is. 如果没有结果,那么不管猫是什么,都希望有结果。

This cannot be done effectively in a single query, you need to use two queries. 在单个查询中无法有效完成此操作,您需要使用两个查询。

If your first query with the condition returns no results, then you attempt the second query, without the cat condition. 如果您的第一个条件查询没有返回结果,则您尝试第二个查询,而没有cat条件。

Best practices 最佳实践

Consider defending yourself against MySQL Injections 考虑防御MySQL注入攻击

Also, please consider using MySQLi or PDO instead of MySQL. 另外,请考虑使用MySQLiPDO代替MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM