$search_query = "SELECT clientid, additionalownerid, additionalpropertyid, officeref, title, firstname, lastname, email1, email2, tel, mobile, identification1, identification2 from clients WHERE clientid = clientid ";
if($clientid != ''){ $search_query .= " and clients.clientid = '$clientid' "; }
if($officeref != ''){ $search_query .= " and clients.officeref like '%$officeref%' "; }
if($title != ''){ $search_query .= " and clients.title like '%$title%' "; }
if($firstname != '' ){ $search_query .= " and clients.firstname like '%$firstname%' "; }
/*
etcetera
*/
$search_query .= " order by lastname"; the conditional where clauses (if statemenst) were not in WHERE , they were part of ORDER BY by the order they were processed