簡體   English   中英

PDO准備查詢-SQL錯誤無法運行查詢:SQLSTATE [42000]

[英]PDO Prepare Query - SQL Error Failed to run query: SQLSTATE[42000]

我在運行此查詢時遇到問題

    //Insert power stats
try {   
    $STH = $db -> prepare(
            "INSERT INTO statsRounds
            (
                version,
                timeFormat,
                mapNumber,
                mapName,
                duration,
                startTic,
                endTic,
                avgEfficiencyPts,
                redPowerPercent,
                bluePowerPercent
            ) 
            VALUES
            (
                $wdlround->versionNumber,
                $wdlround->timeFormat,
                $wdlround->mapNumber,
                $wdlround->mapName,
                $wdlround->durationTics,
                $wdlround->startTic,
                $wdlround->endTic,
                $wdlround->averageEfficiencyPoints,
                $wdlround->redPowerPercent,
                $wdlround->bluePowerPercent
            )"
        );


    //Execute query
    $STH -> execute();      

} //try

catch(PDOException $ex) {
    die("Failed to run query: " . $ex->getMessage());
}

執行后,我收到一個錯誤

Failed to run query: 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 '.22.20.55.00, 6, Lazarus Revisited - SMARTCTF01, 18193, 0, ' at line 17

我檢查以確保值存在於$ wdlround-> versionNumber到$ wdlround-> bluePowerPercent中並且確實存在。 與數據庫的連接也很好。 我也發布了db表,請參見此處: http : //imgur.com/JBvqgGC

我不確定我在做什么錯!

編輯:

新增中

// ECHO QUERY
$mystr = "INSERT INTO statsRounds( 'version', 'time', 'mapNumber', 'mapName', 'duration', 'startTic', 'endTic', 'avgEfficiencyPts', 'redPowerPercent', 'bluePowerPercent') VALUES ( " . $wdlround->versionNumber . ", " . $wdlround->timeFormat . ", " . $wdlround->mapNumber . ", " . $wdlround->mapName . ", " . $wdlround->durationTics . ", " . $wdlround->startTic . ", " . $wdlround->endTic . ", " . $wdlround->averageEfficiencyPoints . ", " . $wdlround->redPowerPercent . ", " . $wdlround->bluePowerPercent . ") ";
echo "MYSTR = $mystr<br>";

結果是

MYSTR = INSERT INTO statsRounds( 'version', 'time', 'mapNumber', 'mapName', 'duration', 'startTic', 'endTic', 'avgEfficiencyPts', 'redPowerPercent', 'bluePowerPercent') VALUES ( 4, 2014.07.22.21.03.11, 6, Lazarus Revisited - SMARTCTF01, 16418, 0, 130136, 107.83333333333, 108.33333333333, 93.333333333333) 

正在輸出

您沒有准備正確的方法,這是應該如何做:

$STH = $db -> prepare(
        "INSERT INTO statsRounds
        (
            version,
            timeFormat,
            mapNumber,
            mapName,
            duration,
            startTic,
            endTic,
            avgEfficiencyPts,
            redPowerPercent,
            bluePowerPercent
        ) 
        VALUES
        (
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?
        )"
    );


//Execute query
$STH -> execute(array(
            $wdlround->versionNumber,
            $wdlround->timeFormat,
            $wdlround->mapNumber,
            $wdlround->mapName,
            $wdlround->durationTics,
            $wdlround->startTic,
            $wdlround->endTic,
            $wdlround->averageEfficiencyPoints,
            $wdlround->redPowerPercent,
            $wdlround->bluePowerPercent
        ));  

根據您的帖子,您的INSERT查詢看起來像

INSERT INTO statsRounds(version, 
time, 
......
) 
VALUES (
 4, 
2014.07.22.20.55.00, <-- Here surround the value with '' 
6, 
......
) 

問題是timeVARCHAR列(按照數據庫架構的發布鏈接),並且其值沒有引號。 您應該像'2014.07.22.20.55.00'這樣傳遞它的值。 同樣,對於所有VARCHAR / CHAR列,將值CHAR在引號中。

您的INSERT語句應如下所示

INSERT INTO statsRounds(
version, 
time, 
mapNumber, 
mapName, 
durstion, 
startTic, 
endTic, 
avgEfficiencyPts, 
redPowerPercent, 
bluePowerPercent) 
VALUES (
'4', 
'2014.07.22.20.55.00', 
6, 
'Lazarus Revisited - SMARTCTF01', 
18193, 
0, 
112948, 
103.66666666667, 
150, 
80) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM