微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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
            ));

我收到此语法错误消息

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 )

它理解概念,只做“ – 关系”,

 请帮忙

但不改变我的表名:)

解决方法:

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

所以你应该使用

 `concept-relation`

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐