Filter a search of a database table in MySQL+PHP to get per search the closest result

Here are my code pieces, which are returning all the records, while I wish to only return the closest matches

  if (isset($_POST['name_query'])){                       
      if(preg_match("/^[ 0-99 a-zA-Z]+/", $_POST['name_query'])) 
            $sql="SELECT  client_id, id_num, name, surname FROM clients WHERE   name LIKE '%" . $name_query."%' " ; 

  else if(isset($_POST['surname_query'])){
      if(preg_match("/^[ 0-99 a-zA-Z]+/", $_POST['surname_query'])){ 
            $sql="SELECT  client_id, id_num, name, surname FROM clients WHERE   surname LIKE '%" . $surname_query."%' " ; 

  elseif (isset($_POST['id_query'])){
      if(preg_match("/^[ 0-99 ]+/", $_POST['id_query'])){ 
            $sql="SELECT  client_id, id_num, name, surname FROM clients WHERE   client_id LIKE '%" . $id_query."%' " ; 


Source: apache

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.