how to update mysql procedure in php mysql? - php

select #i := $priority;
update todo,category set priority = (select #i := #i + 1) where cateid=category.id and status='new' and groups=0 and siteid=1 and priority>=$priority and todo.id!=190;
This statement runs mysql. But not execute in php mysql_query() command. Kindly help me.
Another way tries somthing,
Add Routine : stored procedure concept is not updated my phpmyadmin. It shows #1064 error on indicate the line 'select #'.
DELIMITER //
CREATE PROCEDURE FirstPro(IN st VARCHAR(10), IN grp INT(11),IN sid INT(11),IN pri INT(11), IN tid INT(11))
BEGIN
select #i := pri;
update todo,category set priority = (select #i := #i + 1) where cateid=category.id and status='st' and groups=grp and siteid=sid and priority>=pri and todo.id!=tid
END //
DELIMITER;
It shows
#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' at line 5

Related

Mysql event error using php

I'm trying to initiate a MySQL Event using a PHP script. It works using phpMyAdmin (although I get the same error) but not using the script. I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER' at line 1
DELIMITER |
CREATE EVENT myevent21222
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 MINUTE
DO
BEGIN
UPDATE `team` SET `reg` = '0' WHERE `id` = '1';
END |
# MySQL lieferte ein leeres Resultat zurück (d.h. null Datensätze).
DELIMITER ;
Can anyone figure out the problem?
Is there any alternative for changing data in a database after 5 minutes after a user had done something?
Create the Event:
drop event if exists `myevent21222`;
DELIMITER |
CREATE EVENT myevent21222
ON SCHEDULE EVERY 5 MINUTE STARTS '2016-01-01 00:00:00'
ON COMPLETION PRESERVE
DO
BEGIN
UPDATE `team` SET `reg` = '0' WHERE `id` = '1';
END |
# MySQL lieferte ein leeres Resultat zurück (d.h. null Datensätze).
DELIMITER ;
Turn on the event handler:
SET GLOBAL event_scheduler = ON; -- turn her on and confirm below
Confirm it is on:
show variables where variable_name='event_scheduler';
Check out event info:
show events from so_gibberish2; -- note so_gibberish2 is my database name
-- obviously use your database name above
Look at the manual page for what ON COMPLETION PRESERVE means as well as other things.
Disable or enable it:
ALTER EVENT myevent21222 disable;
ALTER EVENT myevent21222 enable;
Try this.
DELIMITER |
CREATE EVENT myevent21222
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 MINUTE
DO
BEGIN
UPDATE `team` SET `reg` = '0' WHERE `id` = '1';
END ;
# MySQL lieferte ein leeres Resultat zurück (d.h. null Datensätze).
DELIMITER |
you must set the *DELIMITER** first:
see for phpmyadmin
: Creating functions in phpMyAdmin - Error: access denied you need the super privilege for this operation
DELIMITER |
CREATE EVENT myevent21222
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 MINUTE
DO
BEGIN
UPDATE `team` SET `reg` = '0' WHERE `id` = '1';
END |
# MySQL lieferte ein leeres Resultat zurück (d.h. null Datensätze).
DELIMITER ;

create mysql a stored procedure from file

I am trying to create a stored procedure using PHP. My reading indicates the best way to do this by running the .sql file using the 'exec' command in PHP.
In testing i created a file named amtv3_create_routines.sql with this contents:
DELIMITER //
DROP PROCEDURE IF EXISTS createTc //
CREATE PROCEDURE createTc()
BEGIN
drop table if exists v3_tc;
CREATE TABLE v3_tc (
source BIGINT UNSIGNED NOT NULL ,
dest BIGINT UNSIGNED NOT NULL ,
PRIMARY KEY (source, dest) )
ENGINE = InnoDB;
insert into v3_tc (source, dest)
select distinct rel.sourceid, rel.destinationid
from rf2_ss_relationships rel inner join rf2_ss_concepts con
on rel.sourceid = con.id and con.active = 1
where rel.typeid = (select distinct conceptid from rf2_ss_descriptions where term = 'is a')
and rel.active = 1;
REPEAT
insert into v3_tc (source, dest)
select distinct b.source, a.dest
from v3_tc a
join v3_tc b on a.source = b.dest
left join v3_tc c on c.source = b.source and c.dest = a.dest
where c.source is null;
set #x = row_count();
select concat('Inserted ', #x);
UNTIL #x = 0 END REPEAT;
create index idx_v3_tc_source on v3_tc (source);
create index idx_v3_tc_dest on v3_tc (dest);
END //
DELIMITER;
This code works fine when I manually enter it into mysql 5.6.22
However if I save the file and from the prompt enter the command.
mysql -uroot -p -hlocalhost amtv3 < [full path]/amtv3_create_routines.sql
I have tried saving the file using utf8 encoding and windows 1252 encoding.
From the command prompt, there is no feedback, and the procedure is not created.
In PHP I am using the codeigniter framework. If I use the db->query method I can create the stored procedure, however the database loses connection. issuing $db->reconnect() works, but not reliably.
Any suggestions on how to create the stored procedure?
Omitting the space in the last line, DELIMITER; should result in a syntax error (there may be some other reason why this error is not being printed).
DELIMITER is only a feature of certain MySQL clients, and not a feature of the server. Therefore, when executing a .sql file directly, the server will interpret the first semi-colon as the end of the first statement and DELIMITER // will be seen as a syntax violation:
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 'DELIMITER //
CREATE PROCEDURE . . .
Discovered this here:
https://github.com/go-sql-driver/mysql/issues/351#issuecomment-120081608
The Solution?
Simply don't change the delimiter. The BEGIN and END already delimit a compound statement.
Source:
https://www.tutorialspoint.com/mysql/mysql_begin_end_compound.htm
You can try this
mysql -h localhost -U <username> -d <database_name> -f /home/user/<procedure_name>.sql

Create function Error in phpmyadmin

when I try to create a function to retrieve userName from user table using their email it gives me this useless 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 'DECLARE name VARCHAR; BEGIN select userName AS name from
user WHERE `ema' at line 2
, the same code with a different syntax works in mssql so I wonder what is the difference? in better words what am I doing wrong here?
DELIMITER ;;
CREATE FUNCTION getUserName(email varchar(50)) RETURNS VARCHAR
BEGIN
DECLARE name VARCHAR;
SELECT `userName` AS name FROM `user` WHERE `email` = email;
RETURN name;
END ;;
Well, first of all, varchar needs to have length, which you lack in two places. Also, you need to select into the variable and return the variable, because you cannot return resultset. Also you should escape name, and you do not need to alias your column because you are selecting into anyway. So, your code should be like this:
DELIMITER ;;
CREATE FUNCTION getUserName(email VARCHAR(50)) RETURNS VARCHAR(50)
BEGIN
DECLARE NAME VARCHAR(50);
SELECT `userName` FROM `user` WHERE `email` = email
INTO `name`;
RETURN `name`;
END ;;

Is it Possible to declare Cursor within IF Condition in MySQL UDF

Is it possible for me to Declare cursor inside if statement?
if possible how could i make it?
because i just made cursor like this
CREATE FUNCTION `fn_test`(
ProductID BIGINT(20)
)
RETURNS DECIMAL(10,2)
BEGIN
DECLARE PrductDiscValue DECIMAL(10, 2);
DECLARE DiscType INT(1);
DECLARE DiscValue DESIMAL(10,2);
IF ProductID != 0 THEN
SET PrductDiscValue = (SELECT Discountvalue, DiscountType FROM discount WHERE FIND_IN_SET(ProductID,DiscIDs);
END IF;
RETURN ProductDiscountValue(1);
But this is not working. So, I do the following
IF ProductID != 0 THEN
DECLARE PrductDiscValue CURSOR FOR SELECT Discountvalue, DiscountType FROM discount WHERE FIND_IN_SET(ProductID,DiscIDs;
END IF;
OPEN ProductDiscountValue;
FETCH ProductDiscountValue INTO DiscValue, DiscType;
RETURN DiscValue;
END
And this gives me 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 'DECLARE ProdDiscValue CURSOR FOR SELECT Discountvalue, DiscountType FROM discount' at line 16.
I need the both DiscValue and DiscType for different calculation.
Any Help would be appreciate
Thanks in advance.
Please, use it:
BEGIN
IF test = true THEN
BEGIN
DECLARE ats_cursor CURSOR FOR SELECT distinct ats_id FROM ats_eb ats where ats.year = year and ats.edition_shortname = edition;
END;
ELSE
BEGIN
DECLARE ats_cursor CURSOR FOR SELECT distinct ats_id FROM ats_eb ats where ats.year = year and ats.edition_shortname = edition;
END;
END IF;
END
even I never used CURSOR so far in MySQL, But what I found is that , it is clearly mentioned on MySQL manual
Cursor declarations must appear before handler declarations and after
variable and condition declarations.
if you want to use that cursor on some condition then you must use FETCH CURSOR syntax in IF condition
below are some good tutorial which will describe the cursor in details:
CURSOR in mySQL stored procedure
MySQL CURSOR Tutorial

TRIGGER CREATED successfully , but no triggers in information_schema TRIGGERS table

hi i am trying to use triggers , my MySQL version is 5.5.8
the thing is when i create the trigger , PHPMyadmin says it is created successfully ,
this is my trigger
DELIMITER $$
CREATE TRIGGER `check_pupil_before_insert` BEFORE INSERT ON `pupil_data`
FOR EACH ROW BEGIN
IF CHAR_LENGTH( NEW.DateOfBirth ) < 4 THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'check constraint on pupil_data.DateOfBirth failed';
END IF;
END$$
DELIMITER ;
it says
Your SQL query has been executed successfully
DELIMITER $$ CREATE TRIGGER `check_pupil_before_insert` BEFORE INSERT ON `pupil_data`
FOR EACH
ROW BEGIN
IF CHAR_LENGTH( NEW.DateOfBirth ) <4
THEN SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'check constraint on pupil_data.DateOfBirth failed';
END IF ;
but when i
select * from information_schema.triggers
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0843 sec )
SELECT *
FROM information_schema.triggers
LIMIT 0 , 30
why is this happening , please help me , thanks in advance.
Check this.. It will help you ... and make sure query is same as is shown in image change ":=" to "="

Categories