phpmyadmin - Error creating MySQL function (1064) -


phpmyadmin: 3.5.7 mysql: 5.5.29 

i want users able compile several different lists posts in database. following function supposed used add post 1 such list. checks if list id supplied user matches given user id, checks if list not full, adds post list.

delimiter //  create function addtofav(inputkid int, inputuid int, listid int, listlimit int) returns varchar(300)  begin declare resultmsg varchar(300); declare listexists int; set listexists = (select count(*) fav_lists fav_list_id=listid , uid=inputuid , active=1);  if 1 > listexists set resultmsg = 'list non-existant'; else      declare listsize int;          set listsize = (select count(*) fav_links fav_list_id=listid , active=1);     if listsize = listlimit set resultmsg = 'list full';         else if listsize > listlimit set resultmsg ='dberr: list on limit';         else              replace fav_links (kid, fav_list_id, active) values (inputkid, listid, 1);             set resultmsg ='success';         end if; end if;   return resultmsg; end //  delimiter ; 

i enter code phpmyadmin's sql console, returns

#1064 - have error in sql syntax; check manual corresponds mysql server version right syntax use near 'declare listsize int; set listsize = (select count(*) fav_links w' @ line 10 

now, should move "faulty" line up, code looks this:

delimiter //  create function addtofav(inputkid int, inputuid int, listid int, listlimit int) returns varchar(300)  begin declare listsize int; set listsize = (select count(*) fav_links fav_list_id=listid , active=1); declare resultmsg varchar(300); declare listexists int; set listexists = (select count(*) fav_lists fav_list_id=listid , uid=inputuid , active=1); [...] 

this version points same error, on line:

#1064 - have error in sql syntax; check manual corresponds mysql server version right syntax use near 'declare resultmsg varchar(300); declare listexists int; set listexists = (selec' @ line 6 

this leads me believe problem might caused faulty use of delimiter, have got right now. phpmyadmin has delimiter field after sql insertion field, have tried leaving empty, populating ; , //, none of made difference.

what might cause issue, , how fix it?

you should keep variable declarations @ top. took freedom rewrite code:

delimiter //  create function addtofav(inputkid int, inputuid int, listid int, listlimit int) returns varchar(300)  begin declare resultmsg varchar(300); declare listsize int;   if (not exists (select 1 fav_lists fav_list_id=listid , uid=inputuid , active=1))      set resultmsg = 'list non-existant'; else      select count(*) listsize fav_links fav_list_id=listid , active=1;     if (listsize = listlimit)          set resultmsg = 'list full';     elseif (listsize > listlimit)          set resultmsg ='dberr: list on limit';     else          begin             replace fav_links (kid, fav_list_id, active) values (inputkid, listid, 1);             set resultmsg ='success';         end;     end if; end if;  return resultmsg; end //  delimiter ; 

don't use count(*) if want know if exists.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -