title: MySQL & PHP : Multiple Keyword Search
body: <p>I have a string containing comma separated keywords. For Example:</p>
  
  <pre><code>$keywords = 'keyword1, keyword2, keyword3';
  </code></pre>
  
  <p>My Table schema, named <code>tbl_address</code> is like this ( simplified ) :</p>
  
  <pre><code>id        INT(11)        PRIMARY KEY, AUTO INCREMENT
  address   VARCHAR(250)   NOT NULL
  </code></pre>
  
  <p>Assume I have to use <code>MySQLi</code> in PHP ( not <code>PDO</code> ).</p>
  
  <p>Here is my current approach:</p>
  
  <pre><code>$result = array();
  $keyword_tokens = explode(',', $keywords);
  foreach($keyword_tokens as $keyword) {
    $keyword = mysqli_real_escape_string(trim($keyword));
    $sql = "SELECT * FROM tbl_address WHERE address LIKE'%$keyword%'";
    // query and collect the result to $result
    // before inserting to $result, check if the id exists in $result.
    // if yes, skip.
  }
  return $result;
  </code></pre>
  
  <p>This approach works, but inefficient in performance. If there are a lot of keywords, it will make a lot queries. </p>
  
  <p>My question is, is there a better way to achieve the same goal ? i.e. what is the simplest way to return all records with the address containing the ANY of the keywords ?</p>