Can't create stored procedure mysql maybe if else syntax - php

I try to write a stored procedure like this but it does not work:
DROP PROCEDURE IF EXISTS `my_test`;
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF(in_var == 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END
I try to execute from php with PDO
$conn = new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_bd, $mysql_user, $mysql_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec($sql);
no errors but I can't see it on MySql server with
SHOW CREATE PROCEDURE my_test
So I tried to copy paste it in the SQL window of phpmyadmin and I got this syntax error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 'my_in_value') THEN
SET my_out_var = 'my_out_value1'' at line 6
What is the correct syntax? And why PDO not show the error ?
Any help is welcome - thanks

Note, it has been pointed out to me that the below DELIMITER is not necessary in PHPMyAdmin which I don't use. So, DELIMITER is a client thing, such as needed by the likes of MySQL Workbench.
Stored Proc:
DROP PROCEDURE IF EXISTS `my_test`;
DELIMITER $$
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF (my_in_var = 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END$$
DELIMITER ;
Test:
set #outme='';
call my_test('lizard',#outme);
select #outme;
-- my_out_value2
set #outme='';
call my_test('my_in_value',#outme);
select #outme;
-- my_out_value1
So you need to figure out what your intention is with the above.
You had a syntax error with the double =. And you were perhaps having a typo in the in_var that did not exist.
PHPMyAdmin (that does not require DELIMITER so I am told):
DROP PROCEDURE IF EXISTS `my_test`;
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF (my_in_var = 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END

Related

Why can't I create a MySQL stored procedure in PHP?

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 :)
You don't need to redefine Delimiter, when trying to Create a stored procedure using application code.
Also, to avoid exceptions in certain cases, call 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);

SQL error when running command from php file

I need to check if a column exists before trying to add it if not present.
Following information found in this post: mysql ALTER TABLE if column not exists
I added the following to my zen cart php file
$db->Execute("DROP PROCEDURE IF EXISTS `gdpr_accept`;
DELIMITER //
CREATE PROCEDURE `gdpr_accept`()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE " . TABLE_CUSTOMERS . " ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`;
END //
DELIMITER ;
CALL `gdpr_accept`();
DROP PROCEDURE `gdpr_accept`;");
However, I get the following error logged
[05-May-2018 19:37:02 Europe/Paris] PHP Fatal error: 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE PROCEDURE gdpr_accept()
BEGIN
' at line 2 :: DROP PROCEDURE IF EXISTS gdpr_accept;
DELIMITER //
CREATE PROCEDURE gdpr_accept()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE customers ADD gdpr_accept TINYINT(1) NOT NULL DEFAULT '0' AFTER COWOA_account;
END //
DELIMITER ;
CALL gdpr_accept();
DROP PROCEDURE gdpr_accept; ==> (as called by) /Applications/MAMP/htdocs/gdpr/stOrm-PTO-fluSh/includes/init_includes/init_reconsent_popup_setup.php on line 72
However, when I run the same command in phpMyAdmin, after confirming that i want to "DROP PROCEDURE IF EXISTS gdpr_accept" it runs perfectly.
Note: If i attempt to split up the query, it will fail at
$db->Execute("DELIMITER //");
with this error: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //' at line 1
Is there a reason why this SQL command can't be done via php, and is there a way round it?
DELIMITER is a builtin command in the mysql client, it is not a statement that the MySQL server recognizes. You can't run a DELIMITER statement using PHP.
But you don't need to use DELIMITER. That's only to help the mysql client tell where your CREATE PROCEDURE statement ends, because a procedure usually contains semicolon characters, and otherwise it would be ambiguous which semicolon was part of the procedure body versus the end of the procedure definition.
You should run one statement at a time from PHP. Then it's not ambiguous.
$db->Execute("DROP PROCEDURE IF EXISTS `gdpr_accept`");
$db->Execute("CREATE PROCEDURE `gdpr_accept`()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE " . TABLE_CUSTOMERS . " ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`;
END");
$db->Execute("CALL `gdpr_accept`()");
$db->Execute("DROP PROCEDURE `gdpr_accept`;");
By the way, there's no reason you need a procedure for this task, since you just drop the procedure when you're done anyway. It would be much simpler to just run the ALTER TABLE directly:
$db->Execute("ALTER TABLE " . TABLE_CUSTOMERS .
" ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`");
I see a lot of questions on Stack Overflow from people who seem to think it's a good idea to use stored procedures in MySQL. While stored procedures are common in Oracle and Microsoft SQL Server, they're more trouble than they're worth in MySQL. I avoid using stored procedures in MySQL.

mysqlprocedure weird mistake on localhost

I'm trying to change charset of my mysql database using a procedure.
I wrote it few years ago and did not go back to it, till today.
By this I have a weird mistake :
below is my procedure :
Create procedure changecharset()
Begin
Declare tname varchar(255);
Declare done int default 0;
Declare tc Cursor For
Select TABLE_NAME From information_schema.TABLES Where TABLE_SCHEMA = schema();
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
Open tc;
myloop: loop
Fetch tc into tname;
If done = 1 Then
Leave myloop;
End if ;
Set #query = Concat('Alter Table `', tname, '` convert to character set utf8 collate utf8_swedish_ci;');
PREPARE alterstmt FROM #query;
execute alterstmt ;
Deallocate prepare alterstmt;
End loop;
close tc;
End //
Delimiter ;
[/sql]
and this is the answer of mysql server
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '' at line 3
Anykind of help will be much appreciated
It looks like MySQL is seeing the semicolon as the end of the statement.
Make sure you change the delimiter before running the CREATE PROCEDURE statement.
DELIMITER $$
CREATE PROCEDURE foo()
BEGIN
DECLARE ... ;
SET ... ;
END $$
DELIMITER ;

Syntax error when creating trigger from PHP code

I'd like to create a trigger from the following PHP code.
$sql = 'delimiter $$';
$pdo->exec($sql);
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if;
end$$';
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
When I run the the code in MySQL it works and the trigger is created.
PHP shows the following error.
SQLSTATE[42000]: Syntax error or access
violation: 1064 You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'delimiter $$' at line 1
How can I fix it?
Definitely do not try to change the delimiter when you're executing the statement via the API. DELIMITER is a mysql client built-in command, it is not recognized by the MySQL server-side parser.
You don't need it anyway. The purpose of DELIMITER is to remove the ambiguity of semicolons that may appear within the body of a trigger or stored routine. Since the API is for executing one statement at a time, there's no ambiguity. The SQL parser just treats the whole string as one statement anyway.
Likewise, do not end the create trigger statement with $$. You don't need any statement terminator, but the SQL parser accepts ; as an optional statement terminator because so many people put it there even though they don't have to.
The next problem is that you when you use column names in a trigger, you have to prefix them with either NEW. or OLD. -- in an insert trigger, you can only use NEW. If you don't prefix the column, MySQL assumes you meant to set a system variable like tmpdir or slow_query_log.
If you are still getting the 1193 error, I suggest that you didn't change both references to the name column to NEW.name.
I tested the following using PHP 5.4.24 and MySQL 5.6.20, and it worked:
$sql = "create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.name = '' then set NEW.name = null; end if;
end";
$pdo->exec($sql);
You don't need to delimit the column name of name, because it is not a MySQL reserved word. The set of reserved words is documented.
try that :
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if; END;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.`name` = "" then set NEW.`name` = null; end if;
end$$';
$pdo->exec($sql);
Mayby this helps:
$sql = "delimiter //\n";
$pdo->exec($sql);
$sql = "create trigger avoid_empty_employee_insert before insert on employee\n
for each row\n
begin\n
if new.name = '' then\n
set new.name = null;\n
end if;\n
end//\n";
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER. Changing the DELIMITER is important for CLIs.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger `avoid_empty_employee_insert` before insert
on `employee`
for each row
begin
if `name` = "" then set `name` = null; end if;
end';
$pdo->exec($sql);

Can't find error in stored procedure

I can't fint the error in this code? Preciate some help! I also wonder how put values into this stored procedure from PHP/SQL
"
-- SP create new article
DROP PROCEDURE IF EXISTS {$spCreateArticle};
DELIMITER //
CREATE PROCEDURE {$spCreateArticle}
(
IN articleUserId INT,
IN articleSubject CHAR(50),
IN articleText TEXT,
)
BEGIN
INSERT INTO {$tableArticle}
(articleUserId, articleSubject, articleText, articleDate)
VALUES
(spArticleUserId, spArticleSubject, spArticleText, NOW());
END //
DELIMITER ;
",
Error message:
Error code 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER // CREATE PROCEDURE PCreateArticle ( IN articleUserId INT, IN arti' at line 4)
Should IN articleUserId INT etc. instead be IN spArticleUserId INT etc?

Categories