title: Searching multiple columns in mySQL table from PHP array vs fixed
body: <p><strong>Edit: Thanks to spencer7593 for helping me solve this. I have edited the main code to include the correct form for those who come later.</strong></p>
  
  <p>I am currently wrapping my head around PHP and mySQL and have run into a roadblock on searching mySQL.</p>
  
  <p>I need to search multiple columns in a table for partial matches. I have got this working properly with a fixed set of columns to search, but when I try and use an array it fails, even though the output to mySQL seems identical.</p>
  
  <p>Can anyone point out the tiny silly error I have made somewhere? That, or an improved way of doing it?</p>
  
  <pre><code>&lt;form name="search" method="post" action="&lt;?php $PHP_SELF; ?&gt;"&gt;
      Search for: &lt;input type="text" name="find" /&gt;
      &lt;input type="hidden" name="searching" value="yes" /&gt;
      &lt;input type="submit" name="search" value="Search" /&gt;
  &lt;/form&gt;
  
  
  &lt;?php
  
  $dbdata=array(
      'servername'    =&gt; "localhost",
      'username'      =&gt; "Username",
      'password'      =&gt; "Password",
      'dbname'        =&gt; "Membership",
      'table_Members' =&gt; "Members"
  );
  $fields=array(
                  'MemberID',
                  'FirstName',
                  'LastName',
                  'Nickname',
                  'Interests',
                  'Skills');
  
  $output=array();
  if( isset($_POST['searching']) ) {
      $output=read_db_search($dbdata['table_Members'],$_POST['find'],$fields);
  }
  echo ('&lt;pre&gt;');
  var_dump($output);
  echo ('&lt;/pre&gt;');
  
  function read_db_search ($table,$searchQuery,$fields)
  {
      global  $dbdata;
      $servername=$dbdata['servername'];
      $dbname=$dbdata['dbname'];
      $dbtable=$dbdata['table_Members'];
  
      try {
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbdata['username'], $dbdata['password']);
          $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  /*This version works!!!!*/
  $query = "SELECT * FROM `Members` WHERE `"
         . implode("` LIKE '%$searchQuery%' OR `",$fields) 
             . "` LIKE '%$searchQuery%' " ;
  
              /* This section should work but doesnt */
          //$query="SELECT * FROM `Members` WHERE`'.implode(' ` LIKE \'%$searchQuery%\' OR `',$fields).'` LIKE \'%$searchQuery%\'";
          //$stmt = $conn-&gt;prepare($query);
  
  
  
              /* This one works fine */
      /*  $stmt = $conn-&gt;prepare("SELECT * FROM `Members` WHERE
              `MemberID`  LIKE '%$searchQuery%' OR 
              `FirstName`     LIKE '%$searchQuery%' OR 
              `LastName`  LIKE '%$searchQuery%' OR 
              `Nickname`  LIKE '%$searchQuery%' OR 
              `Interests` LIKE '%$searchQuery%' OR 
              `Skills`    LIKE '%$searchQuery%'");
  */
  
  
          $stmt-&gt;execute();
              $result = $stmt-&gt;setFetchMode(PDO::FETCH_ASSOC);
              $data=$stmt-&gt;fetchAll();
          }
          catch(PDOException $e) {
              echo "Error: " . $e-&gt;getMessage();
          }
          $conn = null;
          return $data;
  }
  ?&gt;
  </code></pre>
  
  <p>A var_dump of $query returns:</p>
  
  <pre><code>string(89) "SELECT * FROM `Members` WHERE`'.implode(' ` LIKE \'%searchterm%\' OR `',Array).'` LIKE \'%searchterm%\'"
  </code></pre>
  
  <p>with an error of:</p>
  
  <pre><code>Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%searchterm%\' OR `',Array).'` LIKE \'%searchterm%\'' at line 1
  </code></pre>