PDO准备查询 - SQL错误无法运行查询:SQLSTATE [42000]

时间:2014-07-27 17:38:38

标签: php sql pdo

我在运行此查询时遇到问题

    //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) 

正在输出

2 个答案:

答案 0 :(得分:2)

你没有准备好正确的方法,你应该怎么做:

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

答案 1 :(得分:1)

根据您的帖子,您的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列,请使用引号括起该值。

您的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)