简体   繁体   中英

PHP, MySQL select all if

I have problem with PHP and MySQL please help..

$lokalita_s = $_POST['lokalita_s'];

$query = "SELECT nazov, lokalita FROM reality WHERE lokalita = '".$lokalita_s."' ORDER BY id";

............ But if ($lokalita_s == "nezáleží")... then i want to select every thing from database.. something like this :

$query = "SELECT nazov, lokalita FROM reality ORDER BY id";

............

This is not working :

$lokalita_s = 0;
$lokalita_s = NULL;
$lokalita_s = *;

I really dont want to use it like if else.. because i want to use more variables in that query and it won't be effective

Try

$lokalita_s = $_POST['lokalita_s'];
$wherClause = null;

if($lokalita_s != "nezáleží") {
   $wherClause = "WHERE lokalita = '" . $lokalita_s . "'";
}

$query = "SELECT nazov, lokalita FROM reality $wherClause ORDER BY id";

Something along these lines? (This works in Oracle)

$lokalita_s = $_POST['lokalita_s'];

$query = "
SELECT nazov, lokalita FROM reality WHERE lokalita = '". $lokalita_s."'
UNION 
select nazov,lokalita from reality where '". $lokalita_s. "' = 'nezáleží'
order by id
"

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