使用SQL IF EXISTS时出错

时间:2017-05-20 17:02:03

标签: php mysql sql

运行此PHP脚本时出现SQL错误:

$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();
}

它说:

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

当我在我的计算机上安装XAMPP时,这曾经工作,现在我正在使用带有MySQL 5.7的Ubuntu,这是否与任何SQL语法更改有关?

0 个答案:

没有答案