一个SQL查询取决于用户输入,Wordpress中的简单搜索模块

How to make a query for Search Module based on Wordpress for example. If user types in "name field" do search in Database:

SELECT * FROM db WHERE name LIKE %$_GET["name"]%;

But if user enters text in name and surname field the query should look like:

SELECT * FROM db WHERE name LIKE %$_GET["name"]% AND surname LIKE %$_GET["surname"]%;

Also if user types only in surname field the query should look like:

SELECT * FROM db WHERE surname LIKE %$_GET["surname"]%;

etc. In Laravel I would probably do something like:

$values->where(function($q) use ($_GET["name"], $_GET["surname"]){
 if(isset($_GET["name"]){
 $q->where('name', 'like', '%' . $_GET["name"] . '%');
}
if(isset($_GET["surname"]){
 $q->where('surname', 'like', '%' . $_GET["surname"] . '%');
}
})->get();

But I don't know how to do the same in Wordpress plugin. For Name, surname, phone and email address inputs. Or is it better to grab all db and then search in that array depending on input of user, but also I don't know how to deal with that. :/ Something like:

$database=$mydb->get_results("SELECT * FROM db");
$wholeDb=(array) $database;
and than somethin...

Please help :/