Run mysql "create function" using PHP

I want to run the following mysqlcreate 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 matches the MySQL server version for the correct syntax to use next to "DELIMITER"

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

+5
source share
1 answer

You most likely will not need a team DELIMTER. This applies to MySQL-oriented client programs.

Please try a simple old semicolon:

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 the distinction

CAVEAT

http://php.net/manual/en/mysqli.quickstart.stored-procedures.php

+7

All Articles