I'm trying to create a trigger and its giving me an error that I can't understand the reason of it. In my local server it works perfectly and in the remote doesn't. Both MySQL version are the same, MySQL 5.1.48.
Here is the trigger code:
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
Here is the error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
DELIMITER $$
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
$$
DELIMITER ;
and you probably need IS NOT NULL:
ELSEIF NEW.password IS NOT NULL THEN
Related
I am getting this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
when i run followed code in phpmyadmin also appearing this error beside of second declare expression:
unrecognized statement type (near declare)
What can be cause of these error? (Phpmyadmin, version, etc)
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
Works fine if we use delimiters.
Try this:
DELIMITER $$
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
$$
DELIMITER ;
Why my sql syntax is throwing an error?
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
You don't have delimiter definition before your trigger. Delimiter informs MySQL which characters will end up your trigger. Without it, MySQL assumes semicolon as default delimiter and fails upon first semicolon in your code. Check out this:
DELIMITER $$
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
DELIMITER ;
DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error'
END IF;
END;
$$
DELIMITER ;
Got given error
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF;
END' at line 7
try with this you forget to terminate sql query before end if
DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error';
END IF;
END;
$$
DELIMITER ;
trying to create a stored procedure through mysqli in php. Keep throwing back at 1064 error when I do it through php, but is fine, when I copy the same sql command in php. Anyone able to shed some light on what could be the problem, or anyone found a solution to something similar?
Create Procedure code:
DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END //
DELIMITER ;
PHP Code:
if (!$this->dbc->query("DROP PROCEDURE IF EXISTS " . $this->table_prefix . "makeLive") ||
!$this->dbc->query($query)) {
echo "Stored procedure creation failed: (" . $this->dbc->errno . ") " . $this->dbc->error . "<br /><br />" . $query;
exit;
}
echo "success";
exit;
Script execution is throwing out the following error...
Stored procedure creation failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN tas' at line 1
Although it says it's a syntax error, the fact a straight copy and paste of the command to phpmyadmin seems to work, looks like it's gotta be some sort of mysqli bug perhaps?
The query function only allows you to execute a single query. Try the multi_query function (documentation: http://www.php.net/manual/en/mysqli.multi-query.php ) instead. It allows for multiple commands, and may fix your issue.
Turns out mysqli->query already parses semi-colons and runs the whole command. The delimiter commands at the start and end of the query where unnecessary in successfully creating the procedure.
Just remove DELIMITER and it will work. So in your case it will be like this:
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END;
The table[file_request ] structure:
user_id[INT] file_id[CHAR(10)] all_files
This is the SQL code which work fine in MySQL clien: I am using heidisql
DROP PROCEDURE IF EXISTS check_user_files;
DELIMITER \\
CREATE PROCEDURE check_user_files(IN p_user_id INT,IN p_file_id CHAR(10),IN p_all_files VARCHAR(500))
BEGIN
IF EXISTS(SELECT 1 FROM file_request WHERE user_id = p_user_id AND movie_id = p_file_id) THEN
UPDATE file_request SET `requring_date`=NOW(),`all_files`= p_all_files WHERE `user_id`=p_user_id AND `movie_id`=p_file_id;
ELSE
INSERT INTO file_request (`user_id`,`requring_date`,`movie_id`,`all_files`)
VALUES (p_user_id,NOW(),p_file_id,p_all_files);
END IF;
END \\
DELIMITER ;
CALL check_user_files('23','T40431284','07 08 10 11 13 14');
DELIMITER ;
CALL check_user_files('23','F87951','01 02 03');
And I trying to create and execute the SQL query from PHP [didn't work] :
// create the call procedure statements
foreach($fileData as $key=>$value){
$callSP .= "DELIMITER ; \n
CALL check_user_files('$userID','$key','$value');\n";
}
$insert_file_request_query = "DROP PROCEDURE IF EXISTS check_user_files;
DELIMITER \\\\
CREATE PROCEDURE check_user_files(IN p_user_id INT,IN p_file_id CHAR(10),IN p_all_files VARCHAR(500))
BEGIN
IF EXISTS(SELECT 1 FROM file_request WHERE user_id = p_user_id AND movie_id = p_file_id) THEN
UPDATE file_request SET `requring_date`=NOW(),`all_files`= p_all_files WHERE `user_id`=p_user_id AND `movie_id`=p_file_id;
ELSE
INSERT INTO file_request (`user_id`,`requring_date`,`movie_id`,`all_files`)
VALUES (p_user_id,NOW(),p_file_id,p_all_files);
END IF;
END \\\\
$callSP";
mysqli_query($conn,$insert_file_request_query);
The SQL query which created from PHP didn't work as in the MySQL client!?
So, how can I fix it!?
[update1]
I found that the SQL query must in the special format [ the formate which work fine in the MySQL client] or shouldn't work,I tried to copy and paste the query which echo from the PHP, the query code become one line and couldn't execute in MySQL client,too.
[update2]
The code of create store procedure will work fine when I execute it alone from PHP.I mean,I split the whole process into three parts and execute them one by one.
part1: drop the procedure if it was exists; [using mysqli_query()]
part2: create the procedure;[using mysqli_query()]
part3:call the procedure;[using mysqli_multi_query()]
$insert_file_request_query = '';
foreach($fileData as $key=>$value){
$insert_file_request_query .= "CALL check_save_file_request('$userID','$key','$value');";
}
mysqli_multi_query($conn,$insert_file_request_query);
And my final solution was to create the Store Procedure in MySQL and call it from the PHP.It works fine now.
Thank you very much!!
You can't combine multiple statements in mysqli_query. Split out the definition of the stored procedure from the CALLs to it. If that still fails, we'll need the full and exact error message that you receive.