mysql插入到一个表中,他的名字包含破折号

Wil*_*aan 5 php mysql pdo mysql-error-1064

我正在使用mysql,我有一个表,他的名字是"概念关系",我想插入其中

PHP代码

for ($i = 0; $i < count($sources); $i++) {
    $sourceID = $this->getConcpeptID($sources[$i]);
    $desID = $this->getConcpeptID($distinations[$i]);
    $query2 = "INSERT INTO concept-relation (relationID, firstConceptID, secondConceptID) VALUES (:rID, :sID, :dID)";
    $sth = $this->db->prepare($query2);
    $sth->execute(array(
        ':rID' => $relationID,
        ':sID' => $sourceID,
        'dID' => $desID
    ));
}
Run Code Online (Sandbox Code Playgroud)

我收到此语法错误消息

INSERT INTO concept - relation(
    relationID,
    firstConceptID,
    secondConceptID
)
VALUES ( 3, 3, 3 )
Run Code Online (Sandbox Code Playgroud)

我试图直接从mysql插入并得到错误似乎相同的错误

for ($i = 0; $i < count($sources); $i++) {
    $sourceID = $this->getConcpeptID($sources[$i]);
    $desID = $this->getConcpeptID($distinations[$i]);
    $query2 = "INSERT INTO concept-relation (relationID, firstConceptID, secondConceptID) VALUES (:rID, :sID, :dID)";
    $sth = $this->db->prepare($query2);
    $sth->execute(array(
        ':rID' => $relationID,
        ':sID' => $sourceID,
        'dID' => $desID
    ));
}
Run Code Online (Sandbox Code Playgroud)

问题是因为表的名称在ti中有破折号,看看mysql如何理解查询

INSERT INTO concept - relation(
    relationID,
    firstConceptID,
    secondConceptID
)
VALUES ( 3, 3, 3 )
Run Code Online (Sandbox Code Playgroud)

它理解概念,只做" - 关系",

请帮忙

但不改变我的表名:)

Shi*_*dim 13

用反引号括起标识符会使保留字/字符成为mysql中的有效标识符.

所以你应该使用

 `concept-relation`
Run Code Online (Sandbox Code Playgroud)

反引号(`)是此键盘左上角的键.

在此输入图像描述