It's a bit hacky, but I've handled situations like this with a CASE
statement. You evaluate your argument and if it doesn't meet the condition you require, you use an obviously true statement like 1=1
which has the net effect of keeping that part of the WHERE
clause from participating in filtering the result set.
SELECT *
FROM `db`.`table`
WHERE CASE WHEN TRIM('".$_GET['cl_name']."') IS NOT NULL THEN cl_full_name LIKE '%".$_GET['cl_name']."%'
ELSE 1=1
END
ORDER BY {$oby} {$ohow}
LIMIT {$start_sprava}, {$per_page_sprava}
;
Don't forget to police your inputs so you don't wind up with a Little Bobby Tables problem. Also, the ORDER BY
and LIMIT
clauses generally come after the WHERE
clause.