I've brought two tables together from one database which has a search function applied. At the moment 'photos' are being narrowed down when searching but the contents from 'sponsor' is always displayed. Ideally I want everything to be displayed before searching but then on a search 'sponsor' results will never appear. I've tried to put the $search in the first half with 'photos' but that does the above action. What can I add to stop this?
SELECT * FROM photos
WHERE title LIKE '%".$search."%'
UNION SELECT * FROM sponsor
ORDER BY id DESC
You could use two different SQL statements depending on whether or not $search
is present.
if ($search) {
$sql = "SELECT * FROM photos
WHERE title LIKE '%".$search."%'
ORDER BY id DESC";
} else {
$sql = 'SELECT * FROM photos
UNION SELECT * FROM sponsor
ORDER BY id DESC';
}