带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突:
Posted
技术标签:
【中文标题】带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突:【英文标题】:Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 【发布时间】:2012-09-11 08:02:48 【问题描述】:致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 mysql 服务器版本相对应的手册,以在 C:\xampp\htdocs\phpProject1\userv2.php:120 中的第 1 行的 '") VALUES ()' 附近使用正确的语法 堆栈跟踪:#0 C: \xampp\htdocs\PhpProject1\userv2.php(120): PDOStatement->execute() #1 C:\xampp\htdocs\PhpProject1\create.php(41): User->Save() #2 main 抛出在 C:\xampp\htdocs\PhpProject1\userv2.php 第 120 行
我们在列名中使用了反勾号,正如这里的一些帖子中所建议的那样,但它并不能完全解决错误。我阅读了类似的帖子,但找不到解决方案,因此我正在填写错误和代码以寻求帮助。提前致谢。
<?php
/*
* mmber variable decleration...
*/
class User
private $ID;
private $objPDO;
private $strTableName;
private $arRelationMap;
private $blForDeletion;
private $FirstName;
private $LastName;
private $Username;
private $Password;
private $EmailAddress;
private $DateLastLogin;
private $TimeLastLogin;
private $DateAccountCreated;
private $TimeAccountCreated;
//constructor is use for initialisation for the object
public function __construct(PDO $objPDO, $id = NULL)
$this->strTableName = `system_user`;
$this->arRelationMap = array(
`id` => "ID",
`first_name` => "FirstName",
`last_name` => "LastName",
`username` => "Username",
`md5_pw` => "Password",
`email_address` => "EmailAddress",
`date_last_login` => "DateLastLogin",
`time_last_login` => "TimeLastLogin",
`date_account_created` => "DateAccountCreated",
`time_account_created` => "TimeAccountCreated");
$this->objPDO =$objPDO;
if (isset($id))
$this->ID = $id;
$strQuery = "SELECT ";
foreach ($this->arRelationMap as $key => $value)
$strQuery .= "\"" . $key . "\",";
$strQuery = substr($strQuery, 0, strlen($strQuery)-1);
$strQuery .= "FROM" . $this->strTableName . " WHERE \"id\" = :eid";
$objStatement = $this->objPDO->prepare($strQuery);
$objStatement->bindparam(' :eid', $this->ID, PDO::PARAM_INT);
$objStatement->execute();
$arRow = $objStatement->fetch(PDO::FETCH_ASSOC);
foreach($arRow as $key => $value)
$strMember = $this->arRelationMap[$key];
if(property_exists($this, $strMember))
if (is_numeric($value))
eval('$this->' . $strMember . ' = ' . $value . ';');
else
eval('$this->' . $strMember . ' = "' . $value . '";');
;
;
;
;
public function Save()
if (!isset($this->ID))
$strValueList = "";
$strQuery = 'INSERT INTO "' . $this->strTableName . '"(';
foreach ($this->arRelationMap as $key => $value)
eval('$actualVal = &$this->' . $value .';');
if(isset($actualVal))
var_dump($actualVal);
$strQuery .= '"' . $key . '", ';
$strValueList .= ":$value, ";
;
$strQuery = substr($strQuery, 0, strlen($strQuery) - 2);
$strValueList = substr($strValueList, 0, strlen($strValueList) - 2);
$strQuery .= ") VALUES (";
$strQuery .= $strValueList;
$strQuery .= ")";
unset($objStatement);
$objStatement = $this->objPDO->prepare($strQuery);
foreach ($this->arRelationMap as $key => $value)
eval('$actualVal = &$this->' . $value . ';');
if (isset($actualVal))
if ((is_int($actualVal)) || ($actualVal == NULL))
$objStatement->bindValue(':' . $value, $actualVal, PDO::PARAM_INT);
else
$objStatement->bindValue(':' . $value, $actualVal, PDO::PARAM_STR);
;
;
$objStatement->execute();
$this->ID = $this->objPDO->lastInsertId($this->strTableName . "_id_seq");
【问题讨论】:
能否请您在准备声明之前回显$strQuery
?
首先确定:eval()
是邪恶的!在您使用它的上下文中,这是完全没有必要的,您只需执行$this->$strMember = $value;
即可,它会正常工作。其次,使用反引号是个好主意,但你做错了。反引号应该是 PHP 字符串的一部分,而不是用来引用它。例如`name`
变为 '`name`'
您也没有使用它来引用语句中的字段名称,例如$strQuery .= "\"" . $key . "\",";
应该是 $strQuery .= "`" . $key . "`,";
和 " WHERE \"id\" = :eid"
应该是 " WHERE `id` = :eid"
【参考方案1】:
MySQL INSERT 的正确语法是:
INSERT INTO tbl_name (col1,col2,col3) VALUES(15,col1*2,'string');
而不是:
INSERT INTO "tbl_name" ("col1","col2","col3") VALUES(15,col1*2,'string');
【讨论】:
以上是关于带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突:的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:未捕获的 PDOException:SQLSTATE[42000] 语法错误或访问冲突
我尝试获取时PDOException(PDO :: FETCH_ASSOC)[重复]