title: Debugging error 1064 in SQL in order to see what's failing on a long SQL statment
body: <p>Well, I am running a SQL command that is very large. I am getting error 1064 (Syntax error or access violation).</p>
  <p>Is there any way to get the prepared statement, to see where the fault can be? I'm only seeing 50 characters and I honestly don't know what the bug could be.</p>
  <p>This is how I create and prepare the statement:</p>
  <pre class="lang-php prettyprint-override"><code>function insertIntoMultiple($tablename, $columns, $data, $datetime_column_name = 'created_at') {
      global $db;
      
      $data = json_decode(base64_decode($data), true);
      $exp = explode(&quot;, &quot;, $columns);
      $params = array_diff($exp, [&quot;created_at&quot;]);
      $real_columns = join(&quot;,&quot;, $params);
  
      // $join = join(',', json_decode($columns, true));
      $query = &quot;INSERT INTO $tablename ($real_columns, $datetime_column_name) &quot;;
      $i = 0;
      $c = count($data);
      foreach ($data as $param) {
          $marks = getMarks($params);
  
          // $query = storeQuery(&quot;INSERT INTO $tablename ($join, $datetime_column_name) VALUES ($marks, NOW())&quot;);
          $query .= &quot;VALUES ($marks, NOW())&quot;;
          $query .= $i &lt; $c - 1 ? &quot;,&quot; : &quot;;&quot;;
          $query .= &quot;\n&quot;;
  
          ++$i;
      }
  
      $stmt = $db-&gt;prepare($query);
      $pars = getParams($data, $params);
  
      $r = $stmt-&gt;execute($pars);
  
      return $r;
  }
  </code></pre>
  <p>Where there are 24 columns and 1000 values to insert. So there is 24,000 parameters on the query.</p>
  <p>These are the methods which I use in order to create the string for the query:</p>
  <pre class="lang-php prettyprint-override"><code>function getMarks($params, $for_update = false) {
      $arr = array();
      foreach ($params as $param) {
          if($param == &quot;created_at&quot;) continue;
          $arr[] = $for_update ? '$param = ?' : '?';
      }
      return join(',', $arr);
  }
  
  function getParams($params, $columns) {
      $arr = array();
  
      foreach ($params as $param) {
          foreach ($columns as $column) {
              if($column == &quot;created_at&quot;) continue;
              $arr[] = $param[$column];
          }
      }
  
      return $arr;
  }
  </code></pre>
  <p>For some reason, this isn't working and this is the unique output that I'm getting:</p>
  <blockquote>
  <p><b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[42000]: Syntax
  error or access violation: 1064 You have an error in your SQL syntax;
  check the manual that corresponds to your MariaDB server version for
  the right syntax to use near 'VALUES
  ('1','xxx','xxxx7010','','https://xxxx/xxx/5...' at line 2 in
  /root/project/php/api.php:126</p>
  </blockquote>
  <p>As you can see I can't manage to make it to work with this unique information.</p>
  <p>I already saw this question:</p>
  <ul>
  <li><a href="https://stackoverflow.com/a/3817760/3286975">https://stackoverflow.com/a/3817760/3286975</a></li>
  </ul>
  <p>And I did the following code to output the entire command to a string:</p>
  <pre class="lang-php prettyprint-override"><code>function pdo_debugStrParams($stmt) {
      ob_start();
      $stmt-&gt;debugDumpParams();
      $r = ob_get_contents();
      ob_end_clean();
      
      return $r;
  }
  </code></pre>
  <p>Which gives me the same query (with the marks), because I suppose that this needs to be called after the <code>-&gt;execute()</code> method which is giving me an exception, so I can't see anything.</p>