我在使用事务查询时遇到了一些麻烦 . 我有2个表,“主题”和链接表调用“tutorsubjects” . 我使用的是MariaDB版本10.0.21 . 我创建了以下查询,但我不断收到“语法错误或访问冲突:1064”错误 .
public function addSubject($values){
try {
$temp = $this->db->query("
BEGIN;
INSERT INTO subjects
(subject_code, subject_name, subject_grade, subject_description, subject_category)
VALUES (:subject_code, :subject_name, :subject_grade, :subject_description, :subject_category);
SET @last_id = LAST_INSERT_ID();
INSERT INTO tutorsubject
(tutor_id , subject_id)
VALUES (:tutor_id, @last_id);
COMMIT;",$values);
return $temp;
} catch (DBException $e) {
echo "Error:
" . $e->getMessage();
return null;
} catch (Exception $e) {
echo "Error:
" . $e->getMessage();
return null;
}
}
以下是解析到查询的值
$array = array("subject_code" => $code,
"subject_name" => $subject_name,
"subject_grade" => $grade,
"subject_description" => $subject_description,
"subject_category" => $subject_category,
"tutor_id"=>$selecttutor);
我收到以下错误:
SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以便在''reach'附近使用正确的语法 . 'subject'('subject_code','subject_name','subject_grade','subject_de'在第1行Raw SQL:INSERT INTO'到达' . 'subject'('subject_code','subject_name','subject_grade','subject_description','subject_category')VALUES(:subject_code,:subject_name,:subject_grade,:subject_description,:subject_category);
我的问题是,当我在phpMyAdmin中运行此查询时,它完成没有任何问题 . 我正在使用PDO MySQL类找到here作为我的数据库交互的基础 . 我开始认为也许 class 不直接支持交易?
任何想法将不胜感激 .