How to use MySQL variables in PHP?

46 Views Asked by At

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);
0

There are 0 best solutions below