 <p>You are not using the proper syntax for <a href="http://dev.mysql.com/doc/refman/5.5/en/if.html" rel="nofollow">IF</a>:</p> <pre><code>IF EXISTS ( SELECT * FROM `apples` WHERE `apples`.`color` = ? AND `apples`.`size` = ?) THEN SELECT `apples`.`applesID`; ELSE INSERT INTO `apples` (`color`, `size`) VALUES(?,?); END IF; </code></pre>