8

I want to run the following mysql create function statement from PHP:

DELIMITER $$
CREATE FUNCTION `myFunc`(`instring` varchar(4000)) RETURNS int(11)
    NO SQL
    DETERMINISTIC
    SQL SECURITY INVOKER
BEGIN
    DECLARE position int;
    ....here comes function logic
    RETURN position;
END$$
DELIMITER ;

But I get this mysql error:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER'

What can I do? Can I execute create statement without DELIMITER keyword?

5
  • Don't use the mysql_ functions. They're deprecated for a while now. The two libraries that are still maintained are mysqli and PDO. Commented Jan 28, 2013 at 19:23
  • 2
    MySQL is not deprecated. It is the mysql_* family of functions that are. Use mysqli_* or PDO. Commented Jan 28, 2013 at 19:24
  • Sorry that's what I meant. Edited Commented Jan 28, 2013 at 19:25
  • Bingo (credit to google): stackoverflow.com/questions/5311141/… Commented Jan 28, 2013 at 19:25
  • I am using mysqli, but that's just an adapter. Sql client is still mysql. Commented Jan 28, 2013 at 19:29

1 Answer 1

13

You most likely do not need the DELIMTER command. That belongs to MySQL-centric client programs.

Please try with plain old semicolons:

if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
    !$mysqli->query("CREATE PROCEDURE p(IN id_val INT) BEGIN INSERT INTO test(id) VALUES(id_val); END;")) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

and let PHP worry about delimiting

CAVEAT

I got the above code from http://php.net/manual/en/mysqli.quickstart.stored-procedures.php

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

1 Comment

... "and let PHP worry about delimiting" <-- saved my day :D

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.