title: Using MySQL variable in PHP Prepared Statement
body: <p>When using a SET clause in a MySQL statement, mysqli-&gt;prepare returns false.</p>
  <p>This MySQL statement will &quot;prepare&quot; okay:</p>
  <pre><code>$query = &quot;  INSERT INTO log (channel, message, context, datetime)
                VALUES (?, 'testmsg', '{}', CURRENT_TIMESTAMP); &quot;;
    
    $stmt = $mysqli-&gt;prepare($query); // $stmt will be true
  </code></pre>
  <p>This statement will cause &quot;prepare&quot; to return false.</p>
  <pre><code>$query = &quot; SET @Channel = ?;
             INSERT INTO log (channel, message, context, datetime)
             VALUES (@Channel, '', '{}', CURRENT_TIMESTAMP); &quot;
  $stmt = $mysqli-&gt;prepare($query); // $stmt will be false
  </code></pre>
  <p>The error is:</p>
  <blockquote>
  <p>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 'INSERT INTO log (channel, message, context, datetime) ' at line 2</p>
  </blockquote>
  <p>Why can't prepare handle the SET operator in MySQL in this manner? I want to list all my variables (this example only has one but imagine one with 10+) at the top of my statement for readability instead of sprinkling them inside the mysql code. That's not very unreadable and difficult to debug.</p>