简体   繁体   中英

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. 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'. 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.

Best practices

Consider defending yourself against MySQL Injections

Also, please consider using MySQLi or PDO instead of MySQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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