我无法弄清楚为什么我得到这个MySQL语法错误(PHP PDO)(MySQL)

时间:2017-03-08 10:52:09

标签: php mysql pdo insert

我正在尝试对我的MySQL数据库进行简单的插入,但是我得到了这种语法错误:

Fatal error: Uncaught PDOException: 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
'Combined Authority - ICT Service Desk Technician (WYCA 53) ' at line 2

为什么?

查询:

$conn->exec("INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (" . $name[$i] . "," .  $category[$i] . "," . $contract[$i] 
. "," . $link[$i] . ") AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;"); 

$ sql打印:

INSERT INTO jobs (jobname, category, contract, link) SELECT * FROM ( West
Yorkshire Combined Authority - ICT Service Desk Technician (WYCA 53)
Details ,' Other ',' Other
','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') AS tmp WHERE NOT EXISTS ( SELECT link FROM jobs WHERE link ='https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') LIMIT 1;

为上面的不良格式道歉。请将其复制并粘贴到文本编辑器中以便更好地查看。

编辑:

奇怪的是,这个查询适用于虚拟值,但它仍不适用于数组

INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (SELECT 'Test', 'Test2',
'Test3','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWV
YayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4s
tQ==') AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = 'https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWVYayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4stQ=='
) LIMIT 1;

3 个答案:

答案 0 :(得分:1)

尝试此选择查询并对其他变量执行相同操作:

myUIView.updateConstraints()

答案 1 :(得分:0)

对字符串litterals使用引号 列和表名称的反引号。

更多参考检查:http://php.net/manual/en/pdo.errorinfo.php

答案 2 :(得分:0)

我可以发现三个不同的问题:

  • 首先,您正在发明自己的SQL语法,并且服务器不会被逗乐。你不能SELECT * FROM (anything you want)。您只能从表格,视图或子查询中进行选择。

  • 其次,当您输入例如在SQL中foo,数据库引擎需要一种方法来确定你是指一个表或列,还是你的意思是文字。使用的方法是单引号

    SELECT foo AS this_is_a_column, 'foo' AS this_is_a_value
    FROM bar
    

    您可以在What is the difference between an identifier and a literal?

  • 找到更多详情
  • 最后但并非最不重要的是,您对PDO扩展的整体使用是错误的。 PDO提供了一种分离代码和数据的方法,但您没有使用它。而不是这个:

    $conn->exec("SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;"); 
    

    ......你应该做这样的事情:

    $stmt = $conn->prepared("SELECT link FROM jobs WHERE link=? LIMIT 1"); 
    $stmt->execute($stmt, array($link[$i]));