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

时间:2012-05-15 18:54:19

标签: php mysql pdo mysql-error-1064

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

php code

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

我收到此语法错误消息

Fatal error: Uncaught exception 'PDOException' with message '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 '-relation (relationID, firstConceptID, secondConceptID) VALUES (' at line 1' in C:\xampp\htdocs\Mar7ba\models\ontology_model.php:86 Stack trace: #0 C:\xampp\htdocs\Mar7ba\models\ontology_model.php(86): PDOStatement->execute(Array) #1 C:\xampp\htdocs\Mar7ba\controlers\Ontology.php(69): Ontology_Model->addRelation('jklljkljk', Array, Array) #2 C:\xampp\htdocs\Mar7ba\libs\Bookstrap.php(42): Ontology->relationAdd() #3 C:\xampp\htdocs\Mar7ba\index.php(13): Bootstrap->__construct() #4 {main} thrown in C:\xampp\htdocs\Mar7ba\models\ontology_model.php on line 86

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

#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 '&quot;-&quot;.relation (relationID, firstConceptID, secondConceptID) VALU' at line 1

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

INSERT INTO concept - relation(
relationID,
firstConceptID,
secondConceptID
)
VALUES ( 3, 3, 3 )

它只是理解概念而仅仅是“ - 关系”,

任何帮助,请

但未更改我的表格名称:)

4 个答案:

答案 0 :(得分:11)

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

所以你应该使用

 `concept-relation`

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

enter image description here

答案 1 :(得分:3)

尝试:

"INSERT INTO `concept-relation` (relationID, firstConceptID, secondConceptID) VALUES (:rID, :sID, :dID)";

答案 2 :(得分:2)

试试这个

$query2 = "INSERT INTO concept-relation (relationID, firstConceptID, secondConceptID)
                VALUES (:rID, :sID, :dID)";

答案 3 :(得分:2)

$query2 = "INSERT INTO `concept-relation` (relationID, firstConceptID, secondConceptID)
                VALUES (:rID, :sID, :dID)";