I tried to create a stored prucedure with the following code:
DELIMITER //
CREATE PROCEDURE AddTrickBaseInvert(
IN invertName VARCHAR(16),
IN invertType TINYINT(1)
)
BEGIN
INSERT INTO trick_bases (type)
VALUES (1);
INSERT INTO trick_bases_invert (
baseId,
name,
type
)
VALUES (
LAST_INSERT_ID(),
name,
type
);
END //
DELIMITER ;
It worked when I tried to run a MySQL test on localhost/phpmyadmin/, but when I tried to create the same procedure using the identically same SQL code via PHP, it gave me the following error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $$ CREATE PROCEDURE AddTrickBaseInvert( IN invertName ' at line 1 ...
The PHP code is here:
$sql = '
DELIMITER //
CREATE PROCEDURE AddTrickBaseInvert(
IN invertName VARCHAR(16),
IN invertType TINYINT(1)
)
BEGIN
INSERT INTO trick_bases (type)
VALUES (1);
INSERT INTO trick_bases_invert (
baseId,
name,
type
)
VALUES (
LAST_INSERT_ID(),
name,
type
);
END //
DELIMITER ;';
$oDb->exec($sql);
As you see, the SQL code is exactly the same, but somehow the procedure does not get created. I tried to do researches but I could not find any answers yet. Do I miss something essential from the codes above which is somehow included when I run the code in the SQL tab on localhost/phpmyadmin?
Thanks in advance :)
DROP PROCEDURE IF EXISTS
before the Create procedure statement. Do the following instead:
// drop if procedure exists already or not
$sql_drop = 'DROP PROCEDURE IF EXISTS AddTrickBaseInvert';
$oDb->exec($sql_drop);
// Now call create procedure statement
$sql = '
CREATE PROCEDURE AddTrickBaseInvert(
IN invertName VARCHAR(16),
IN invertType TINYINT(1)
)
BEGIN
INSERT INTO trick_bases (type)
VALUES (1);
INSERT INTO trick_bases_invert (
baseId,
name,
type
)
VALUES (
LAST_INSERT_ID(),
name,
type
);
END';
$oDb->exec($sql);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.