0

I am trying to do something like this:

SET @grade_id = (SELECT  grade_id FROM grades_table WHERE grades = '1' AND sections = 'A');
SET @subject_id = (SELECT subject_id FROM subjects_table WHERE grade_id = @grade_id AND subject_name = 'Maths' AND teacher_id = '1');
INSERT INTO topics_table ( subject_id, grade_id, topic_name) VALUES (@subject_id, @grade_id,'Another Topic'); 

This works in phpmyadmin but doesn't work when I use it with PHP in Visual Studio Code. I'm doing this in VSC:

$topic = "SET @grade_id = (SELECT  grade_id FROM grades_table WHERE grades = '1' AND sections = 'A');
SET @subject_id = (SELECT subject_id FROM subjects_table WHERE grade_id = @grade_id AND subject_name = 'Maths' AND teacher_id = '1');
INSERT INTO topics_table ( subject_id, grade_id, topic_name) VALUES (@subject_id, @grade_id,'Another Topic')"; 

mysqli_multi_query(
    $connection,
    $topic
);
mysqli_close($connection);
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • I don't have a place to test this right now, however it appears as though $topic should be an ongoing concatenation, not three lines separated by \n or whatever this is using. try: `$topic = "set...` `$topic .= "set...` `$topic .= "insert...` I'm not sure it matters, but maybe a line break is causing the issue. Otherwise, to my knowledge, this looks fine. – Clayton Engle Aug 31 '21 at 20:42
  • Also, just as a quick aside, if you have access you could create these as stored procedures, and then use a single query to execute them using CALL. – Clayton Engle Aug 31 '21 at 20:45
  • **Never use `mysqli_multi_query()`** – Dharman Sep 01 '21 at 09:49

0 Answers0