A simple way would be to use a PHP framework like CodeIgniter or Laravel which have in-built features like filtering and active-record, so that you don't have to worry about these nuances.
#Edit:-
You need not use any framework only for features like query building or Active record, as several independent libraries are also available. For example check this github repository SQL-Query-Builder