mysql PDO多参数插入语句

时间:2013-02-12 20:36:29

标签: php mysql pdo

我不知道出了什么问题。我已经看到了很多不同的方法,有人可以告诉我这到底出了什么问题......它抛出这个错误“错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在'desc'附近使用正确的语法VALUES('30 .59','blue shirt','a really冷静的蓝色衬衫')'在第1行“。我尝试了多种方式,结果相同......

<?php
$title = 'blue shirt';
$desc = 'a really cool blue shirt';
$price = 30.59;

$user = 'foo';
$pass = 'Bar';

try{
$conn = new PDO('mysql:host=examplehost;dbname=exampledb_name',$user,$pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!$conn){
    echo"couldnt connect to db";
}else {
    echo 'connected like a boss!!' . '<br>';
    $stmt = $conn->prepare("INSERT INTO 68_items (price, title, desc) VALUES (:price,:title,:desc)");
    if(!$stmt->execute(array(
            ':price' => $price,
            ':title' => $title,
            ':desc' => $desc))
    ) {
        echo'statment failed';
    }else {
        echo 'statment success, ' .  $stmt->rowCount() . 'rows affected.';
    }
}
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>

1 个答案:

答案 0 :(得分:6)

descreserved word in mysql所以你应该使用反引号来逃避它:

"INSERT INTO 68_items (price, title, `desc`) VALUES (:price,:title,:desc)"