title: Delete from a table after checking if it exists
body: <p>I need to check if a table exists before deleting a row from it in sql. I have tried several things off the internet and this seems to come up the most, but it is not working.</p>
  
  <pre><code>IF OBJECT_ID('mytable', 'N') IS NOT NULL
  BEGIN
      DELETE FROM table WHERE user_id= :user_id
  END
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>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 'IF OBJECT_ID('we_image_gallery', 'U') IS NOT NULL DELETE FROM mytable' at line 1 
  </code></pre>
  
  <p>I need a solution that uses only the current database and I can count on when the script is ported to multiple systems.</p>
  
  <p>Edit: This query is ran by PHP PDO. Beings none of the answers seem to working is it possible there is a settings in my pdo or sql server that's causing the issue?</p>