title: MySQL UPDATE, IF, INSERT INTO statement
body: <p>I am trying to follow the following <a href="https://blogs.msdn.microsoft.com/miah/2008/02/17/sql-if-exists-update-else-insert/" rel="nofollow noreferrer">blog</a>  and use an UPDATE, IF, INSERT INTO statement seeing that it will no go over my data twice.</p>
  
  <p>Please note it is for php.</p>
  
  <p>The statement that I have is as follows</p>
  
  <pre><code>$query = "UPDATE 
              " . $this-&gt;table_name2 . "
          SET  
              batch = :batch, 
              created = :created
          WHERE 
              id = :id
          IF row_count() = 0 
          INSERT INTO " . $this-&gt;table_name2 . "
          SET 
              id=:id, 
              batch=:batch, 
              created=:created";
  </code></pre>
  
  <p>But my return value always comes back as false, and I do not know where the problem is.</p>
  
  <p>If I try the first half of the statement it updates the information:</p>
  
  <pre><code>$query = "UPDATE 
              " . $this-&gt;table_name2 . "
          SET  
              batch = :batch, 
              created = :created
          WHERE 
              id = :id
  </code></pre>
  
  <p>And if I try the second half of the statement it INSERTS the information:</p>
  
  <pre><code>        INSERT INTO " . $this-&gt;table_name2 . "
          SET 
              id=:id, 
              batch=:batch, 
              created=:created";
  </code></pre>
  
  <p>I do not find any help after allot of searching so far, and I feel that somehow my IF statement may not be correct, but I do not even get info on the <code>IF row_count() = 0</code> in the docs.</p>