我有这个网站,我正在研究匹配两个对象,我找到了一个配对功能,为每个比赛生成一个唯一的ID号。这一切都很好,但是我不想在数据库中填入7000个独特的数字。所以,我想到了一个主意。我将使用PHP和while循环自动插入这些值。不幸的是,我遇到了很多问题。这是我到目前为止的代码:
<?php
$db = new PDO('mysql:host=hostname;dbname=db_name;charset=utf8', 'username', 'pasword');
$x = 2;
$y = 1;
while ($x <= 117) {
$num = ((pow($x,2) + (3*$x) + (2 * $x * $y) + $y + pow($y,2))/2);
echo $num ."<br>";
$sql = "INSERT INTO votes (match) VALUES (:match)";
$q = $db->prepare($sql);
$q->execute(array(':match'=>$num));
$x = $x + 1;
}
?>
当我尝试运行时,我得到了
致命错误:
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 'match) VALUES (?)' at line 1' in
/home/content/70/11524070/html/matchup/math.php:11 Stack trace: #0
/home/content/70/11524070/html/matchup/math.php(11):
PDO->prepare('INSERT INTO vot...') #1 {main} thrown in
/home/content/70/11524070/html/matchup/math.php on line 11
关于如何使其发挥作用的任何想法?
答案 0 :(得分:1)
您收到此错误是因为MATCH
是mysql中的保留字。您有两种方法可以解决此问题:
1。)将列的名称更改为match
以外的其他名称。 (推荐)
2.。)在sql字符串中使用反引号:
$sql = "INSERT INTO `votes` (`match`) VALUES (:match)";
答案 1 :(得分:-1)
Escape使用反引号的表名和列名:
$sql = "INSERT INTO `votes` (`match`) VALUES (:match)";