title: SQLSTATE[42000] Exception while executing multiple API calls & creating many temp tables
body: <p>Due to many limitations, I'm only able to use vanilla PHP. The current API is plain and simple, it has two tables, one is for stats, the other includes the records.</p>
  
  <p>First, stats table will be fetched: </p>
  
  <pre><code>$tempStatsName = $this-&gt;getRandomIndexString();
  $sql = "CREATE TEMPORARY TABLE IF NOT EXISTS " . $tempStatsName . " AS SELECT COUNT(*) AS total_visits, series_id FROM my_stats_table d2 WHERE d2.stats_type = 1 GROUP BY series_id;";
  $this-&gt;conn-&gt;exec($sql);
  </code></pre>
  
  <p>Then, an index will be added </p>
  
  <pre><code>$sql = "ALTER TABLE `" . $tempStatsName . "`  ADD INDEX `" . $this-&gt;getRandomIndexString() . "`  (`series_id`);";
  $this-&gt;conn-&gt;exec($sql);
  </code></pre>
  
  <p><code>getRandomIndexString</code> is a function that generates a unique random string.</p>
  
  <pre><code>return substr(str_shuffle(MD5(microtime())), 0, 10)
  </code></pre>
  
  <p>Finally, the last query will be executed: </p>
  
  <pre><code>SELECT database_series.id, database_stats.total_visits,(SELECT COUNT(*) FROM myreviewstable d_reviews WHERE d_reviews.media_id = database_series.id AND d_reviews.review_media_type = 0) series_reviews_count, (SELECT TRUNCATE(AVG(d_reviews2.review_rating_value), 1) FROM myreviewstable d_reviews2 WHERE d_reviews2.media_id = database_series.id AND d_reviews2.review_media_type = 0) series_reviews_value, database_series.series_title, database_series.series_description, database_series.series_thumbnail, (SELECT COUNT(*) FROM myepisodestable d_episodes WHERE d_episodes.episode_series = database_series.id) series_episodes_count FROM myrecordstable database_series LEFT JOIN " . $tempStatsName . " database_stats ON (database_stats.series_id = database_series.id)
  </code></pre>
  
  <p>Exception: </p>
  
  <pre><code> 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 MySQL server version for the right syntax to use near '189e087f9e0ce79e31b4 AS SELECT COUNT(*)
  </code></pre>
  
  <p>Another example: </p>
  
  <pre><code>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 '2332e22834 database_stats ON (database_stats.series_id = database_series.id) WHE'
  </code></pre>
  
  <p>This exception only happens when I request a call to the API 3x times in a row (in less than 2s due to lazy-scroll methods being called).</p>
  
  <p>If I wait a few seconds and then try scrolling, the API will be called and no exception will be shown. </p>
  
  <p>I'm unable to know why the exception happens, are there any duplicated temp tables? I'm pretty sure it's not an issue with the <code>max_heap_size</code>, the records are less than 100.</p>
  
  <p>The important parts of the database are the stats &amp; records tables, other tables are not important and didn't cause any issues.</p>
  
  <p><code>my_stats_table</code>: </p>
  
  <pre><code>id stats_type series_id created_at created_at_text
  </code></pre>
  
  <p><code>myrecordstable</code>:</p>
  
  <pre><code>id series_title created_at
  </code></pre>
  
  <p>Basically, the three queries are executed in the same connection and are included in a function called <code>getSeries()</code>. It executes the first, which creates the temp table, after that alters the table, finally, it executes the code which fetches the records of <code>myrecordstable</code>.</p>