Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute mysql "create function" statement with PHP

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?

like image 783
Borut Tomazin Avatar asked Jan 28 '13 19:01

Borut Tomazin


1 Answers

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

like image 68
RolandoMySQLDBA Avatar answered Oct 02 '22 23:10

RolandoMySQLDBA