You can write multiple SQL statements in one query so you can use the previous value of the same query to the next.
e.g.
q ="DELCARE @teacherid int, @publicationid int;
SET @teacherid = SELECT id from teachertable;
SET @publicationid = SELECT id from publicationstable where teacherid = @teacherid;
INSERT INTO sometable (blah, blah) VALUES(@teacherid, @publicationid )";
execute query q and you are done. All in one statement