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.
Related
I want to add a trigger that insert the new state of my ride entity into the ride_history entity:
DELIMITER //
CREATE TRIGGER after_updating_changes
AFTER UPDATE ON ride
FOR EACH ROW
BEGIN
IF OLD.status_id <> NEW.status_id THEN
INSERT INTO ride_history(`ride_id`, `status`, `created`)
VALUES(NEW.id, NEW.status_id, NOW());
END IF;
END //
DELIMITER ;
I already tested it on phpmyadmin and there was no error during execution, but when I put it into my migration file and try to migrate the database, symfony returns this error:
An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL synt
ax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER // CREATE TRIGGER after_
updating_changes AFTER UPDATE ON ride FOR E...' at line 1
So i'm a bit lost into all this, please help me!
DELIMITER is client command, not SQL statement. So it causes an error while using as SQL statement.
Convert your trigger text to single-statement form making DELIMITER command unneeded:
CREATE TRIGGER after_updating_changes
AFTER UPDATE ON ride
FOR EACH ROW
INSERT INTO ride_history(`ride_id`, `status`, `created`)
SELECT NEW.id, NEW.status_id, NOW()
WHERE OLD.status_id <> NEW.status_id;
PS. Take into account that if any status_id value is NULL then none will be saved into the history table. So you must adjust WHERE condition accordingly if this column is nullable.
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 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.
SQL Query -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`)
VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END;
DELIMITER ;
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 '' at line 3
Need help... thanks in advance :)..
For your case you can use this statement -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
What is the DELIMITER in MySQL and what it’s used for.
Delimiter problems I reckon:
DELIMITER $$
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END$$
DELIMITER ;
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?