title: Search last 4digits and match the exact or nearest result in the database using php
body: <p>I'm working in a project where i need to search for the last 4digit of a mobile number and display the exact match or if not suggest the nearest number to it. </p>
  
  <p>Example: 
  if user will search for 8773302 it match the 4digit from the database, it will display 8173302 (from database)</p>
  
  <p>else if not found, it will display other suggestions like:
  8173301, 8173303, 8173304, 8173305 and so forth. </p>
  
  <p>CODE:</p>
  
  <pre><code>$existNo = "8673302";
  $search = substr($existNo, -4);
  $search_exploded = explode (" ", $search);
  echo $plan;
  
  foreach($search_exploded as $search_each)
  {
      $x++;
      if($x==1)
      $construct .="mobileno LIKE '%$search_each%'";
      else
      $construct .="AND mobileno LIKE '%$search_each%'";
  }
  
  $constructs ="SELECT * FROM mobilenum WHERE $construct OR status = '0'";
  $run = mysql_query($constructs);
  
  $foundnum = mysql_num_rows($run);
  
  if ($foundnum==0)
  {
      header ("LOCATION:suggestedNo.php");
  }
  else
  { 
      $getquery = mysql_query("SELECT * FROM mobilenum WHERE $construct") or die (mysql_error());
      //Table Results 
  
      while($runrows = mysql_fetch_assoc($getquery))
      {
          $verifyMobileNo= $runrows ['mobileno'];
          $verifyMobileStat= $runrows ['status'];
      }
  
      $matchMobileNumber = $verifyMobileNo;
      if ($verifyMobileStat == "0"){
          header ("LOCATION:matchNo.php");
      }
      else {
          header ("LOCATION:suggestedNo.php");
      }
  }
  </code></pre>
  
  <p>Hope somebody can help thanks a lot in advance</p>