user1424332
user1424332

Reputation: 255

Executing .sql file with variable from PHP script

I have very long SQL file which includes $variable in format like:

DELIMITER $$
DROP TRIGGER IF EXISTS `$variable` $$
    CREATE TRIGGER `TEMP` BEFORE INSERT ON `5_2012`
    FOR EACH ROW BEGIN
      set NEW.chour = timediff(NEW.exit, NEW.enter);
      set NEW.total = timediff(NEW.exit, NEW.enter);
...

I need a way to execute this script each time with different table names ($variable).

I am using MySQL.

Upvotes: 0

Views: 1044

Answers (1)

Jared
Jared

Reputation: 406

Change your .sql file into a .php script Add a header to the top, and add the mime type. If i can remember correctly, it's application/x-sql. Echo out each variable.

<?php
header("Content-type: application/x-sql");
?>

DELIMITER $$ 
DROP TRIGGER IF EXISTS `<?= $variable; ?>` $$
CREATE TRIGGER `TEMP` BEFORE INSERT ON `5_2012` 
FOR EACH ROW BEGIN   
    set NEW.chour = timediff(NEW.exit, NEW.enter);
    set NEW.total = timediff(NEW.exit, NEW.enter); 

Upvotes: 1

Related Questions