0

I'm having a SQL error when I run this PHP Script:

$JSONpricelist = file_get_contents("https://api.csgofast.com/price/all");
$Pricelist = json_decode($JSONpricelist);

$sth = $conn->prepare( "IF EXISTS(SELECT MarketName FROM Skins WHERE MarketName=:MarketName)
                    THEN
                        UPDATE SkinPrices SET BuyPrice=:BuyPrice, SellPrice=:SellPrice WHERE SkinMarketName=:MarketName;
                    ELSE
                        INSERT INTO Skins (MarketName) VALUES (:MarketName);
                        INSERT INTO SkinPrices (SkinMarketName, BuyPrice, SellPrice) VALUES (:MarketName, :BuyPrice, :SellPrice);
                    END IF;");
$sth->bindParam(':BuyPrice', $buyPrice);
$sth->bindParam(':SellPrice', $sellPrice);
$sth->bindParam(':MarketName', $market_name);

foreach($Pricelist AS $market_name => $price) {
    $buyPrice = $price * 100 - 3;
    $sellPrice = $price * 107 + 3;
    $sth->execute();
}

It says:

Fatal error: 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 'IF EXISTS(SELECT MarketName FROM Skins WHERE MarketName='★ Huntsman Knife ' at line 1 in /var/www/csgoaced.xyz/public_html/lib/database/UpdateSkinPrices.php:20 Stack trace: #0 /var/www/csgoaced.xyz/public_html/lib/database/UpdateSkinPrices.php(20): PDOStatement->execute() #1 /var/www/csgoaced.xyz/public_html/lib/controller/CSGOAcedController.php(16): require('/var/www/csgoac...') #2 /var/www/csgoaced.xyz/public_html/index.php(1): require('/var/www/csgoac...') #3 {main} thrown in /var/www/csgoaced.xyz/public_html/lib/database/UpdateSkinPrices.php on line 20

This used to work when I had XAMPP on my computer, now I'm using Ubuntu with MySQL 5.7, is this related to any SQL syntax change?

  • 3
    Possible duplicate of [Usage of MySQL's "IF EXISTS"](http://stackoverflow.com/questions/5528854/usage-of-mysqls-if-exists) – rackemup420 May 20 '17 at 17:06
  • 2
    I think the issue you are having is that long form IF syntax only works in stored procedures or functions. It is possible that it worked otherwise in older versions of MySQL, but 5.7 is more strict. – D Lowther May 20 '17 at 17:07

0 Answers0