MySQL Error querying from PHP: IF EXISTS

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, MySQL Tutorial  //  No Comments

I have this PHP Code with Query to mysql database:

$query2 = "IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='$shipid' AND item='$item' AND price='$price')) 
    BEGIN
    UPDATE shipcargo SET amount = amount+'$amount' WHERE shipid='$shipid' AND item='$item' AND price='$price'
    END
    ELSE
    BEGIN
    INSERT INTO shipcargo (shipid, item, amount, price) VALUES('$shipid', '$item', '$amount', '$price')
    END";
    mysql_query($query2) or die(mysql_error());

but The Error Returned is:
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( EXISTS (SELECT * FROM shipcargo WHERE shipid=’11’ AND item=’WheatBastard’ AN’ at line 1

Here’s the code:

IF EXISTS (SELECT * FROM  

Leave a comment

Share This Post

RSS Wordpress News