3

I have this code

DELIMITER $$
DROP FUNCTION IF EXISTS `GetNextID` $$
CREATE FUNCTION `GetNextID`() RETURNS INT DETERMINISTIC
BEGIN
DECLARE NextID INT;
SELECT MAX(articleID) + 5 INTO NextID FROM table_article;
RETURN NextID;
END$$
DELIMITER ;

INSERT INTO table_article ( articleID, articleAlias ) VALUES ( GetNextID(), 'TEST' );

executed OK in phpMyAdmin, but it fails when i pass this query to mysql_query PHP function/ Me guess this is because of the function and semi-colons. What do i do?

1
  • It would help to post your code and the error you're getting. Commented Apr 14, 2011 at 11:45

2 Answers 2

2

DELIMITER is not a MySQL keyword: it is a reserved word parsed by clients (like mysql, phpMyAdmin etc.) which allows splitting the queries.

You should split it manually and submit the three queries:

DROP FUNCTION IF EXISTS `GetNextID`

,

CREATE FUNCTION `GetNextID`() RETURNS INT DETERMINISTIC
BEGIN
DECLARE NextID INT;
SELECT MAX(articleID) + 5 INTO NextID FROM table_article;
RETURN NextID;
END

and

INSERT INTO table_article ( articleID, articleAlias ) VALUES ( GetNextID(), 'TEST' )

in three separate calls to the database.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, i was going to, but stackoverflow told me to wait for 3 minutes, though ;) here you go...
0

you have DECLARE NextID INT; and RETURN NextID; and another line with ; inside the DELIMITER $$ deceleration.

my advice is stop using $$ as a delimiter

1 Comment

what to use instead? can you rewrite the code the right way, pls? =]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.