tags:

views:

92

answers:

1

I am using following PHP code for trigger creation but always get error, please help me to resolve it.

$link = mysql_connect('localhost','root','rainserver');
    mysql_select_db('information_schema');
                echo $trgquery = "DELIMITER $$ DROP TRIGGER `update_data` $$ CREATE TRIGGER `update_data` AFTER UPDATE on `jos_menu` FOR EACH ROW BEGIN
IF (NEW.menutype != OLD.menutype) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.menutype, newvalue = NEW.menutype, field = 'menutype';
END IF;
IF (NEW.name != OLD.name) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.name,

newvalue = NEW.name, field = 'name';
END IF;
IF (NEW.alias != OLD.alias) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.alias,

newvalue = NEW.alias, field = 'alias';
END IF;
END$$ DELIMITER ;";
                echo "<br>";
        //$trig = mysqli_query($link,$trgquery) or die("Error Exist".mysqli_error($link));
         $trig = mysql_query($trgquery) or die("Error Exist".mysql_error());

I get the error as:

Error ExistYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$$ DROP TRIGGER `update_data` $$ CREATE TRIGGER `update_data` AFTER UPDATE on `j' at line 1

PLease help me to create my trigger...

+1  A: 

The error tells you exactly what's wrong. DELIMITER $$ DROP ... isn't a valid statement. Part of the reason for this is delimiter is a MySQL command, not a statement. Another reason is the "$$" in "DELIMITER $$" is part of the command and thus doesn't terminate the command. You still need to include the old delimiter.

According to the documentation, mysql_query doesn't support multiple statements, so you'll have to issue separate queries for each one. Better yet, switch to the newer mysqli driver, whose multi_query function works for multiple statements in a single query. mysqli also supports prepared statements, which doesn't matter so much in your example, but does in most other situations. PDO is even newer and also supports prepared statements, and I believe the PDO MySQL driver supports multi-queries.

As an alternative to triggers, row-based binary logging will record changes made to tables in a database.

outis
Thanks Outis.. Ur Guide resolved my problem, I executed the queries separately... Thanks Again
OM The Eternity