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.
Related
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);
I'm working on a php project and i'm still a novice in error debugging.
whenever i try to execute the following code
CREATE PROCEDURE insertData(Name varchar(255),Description text)
BEGIN
INSERT INTO categories(name,description) VALUES (Name,Description);
END
i get an error stating (in phpmyadmin)
MySQL said: Documentation
#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 '' at line 3
Try using a DELIMITER statement:
DELIMITER $$
CREATE PROCEDURE insertData (
in_Name varchar(255),
in_Description text
)
BEGIN
INSERT INTO categories(name, description)
VALUES (in_Name, in_Description);
END;$$
DELIMITER ;
Notice that I also renamed in the input parameters so they are less likely to be confused with column names. This is a good practice when writing stored procedures and functions.
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
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?
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
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?