title: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO
body: <p>I've looked through all the other StackOverflow (and google) posts with the same problem, but none seemed to address my problem.</p>
  
  <p>I am using PDO and PHP.</p>
  
  <p>My code:</p>
  
  <pre><code>$vals = array(
     ':from'    =&gt; $email,
     ':to'      =&gt; $recipient,
     ':name'    =&gt; $name,
     ':subject' =&gt; $subject,
     ':message' = &gt;$message
  );
  print_r($vals);
  try {
     $pdo = new PDOConfig();
     $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $sql = "SELECT * FROM messages WHERE `message` LIKE :message";
     $q = $pdo-&gt;prepare($sql);
     $q-&gt;execute(array(':message' =&gt; $vals[':message']));
     $resp = $q-&gt;fetchAll();
  
     foreach ($resp as $row) {
        throw new Exception('Please do not post the same message twice!');
     }
  
     $sql = "INSERT INTO messages (from, to, name, subject, message) VALUES (:from, :to, :name, :subject, :message)";
     $q = $pdo-&gt;prepare($sql);
     $q-&gt;execute($vals);
  } 
  catch(PDOException $e) {
     echo $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>and the first print_r gives</p>
  
  <pre><code>Array ( [:from]    =&gt; abc@gmail.com 
          [:to]      =&gt; lala@me.com 
          [:name]    =&gt; abc 
          [:subject] =&gt; abc 
          [:message] =&gt; abc )
  </code></pre>
  
  <p>which is expected (none are null)</p>
  
  <p>but it outputs the error</p>
  
  <blockquote>
    <p>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 'from, to, name, subject, message) VALUES ('abc@gmail.com', 'lala@me.com' at line 1</p>
  </blockquote>
  
  <p>No idea how to fix this. any ideas?</p>