I'm working on login/register module of my program based on a tutorial I found. Everything worked fine but then I switched from MSSql to MySql database.
The problem is I can't rewrite my InsertUser procedure. This procedure takes 3 parameters (username, password and email) and can return 3 different values
-1 if username is already used
-2 if email is already used
id of new row if registration is successful
I tried to write it like this:
DELIMITER $$
CREATE PROCEDURE InsertUser(
IN username VARCHAR(50),
IN pass VARCHAR(255),
IN email VARCHAR(50))
BEGIN
IF (SELECT Id FROM Users WHERE UserName=username) THEN
BEGIN
SELECT -1;
END;
ELSEIF (SELECT Id FROM Users WHERE Email=email)
THEN
BEGIN
SELECT -2;
END;
ELSE
BEGIN
INSERT INTO Users (UserName, Password, RegDate, Email) VALUES(username, pass, CURDATE(), email);
SELECT LAST_INSERT_ID();
END;
END IF;
END $$
DELIMITER ;
When I try to create this procedure using the above code from Visual Studio I get error (wrong syntax). However there is no error if I do it from phpmyadmin page. But it doesn't work at all (returns nothing) regardless of arguments I provide. I'm using phpmyadmin page and execute procedure option to test it.
Here is original T-Sql code:
CREATE PROCEDURE [dbo].[Insert_User]
@Username NVARCHAR(20),
@Password NVARCHAR(20),
@Email NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT Id FROM Users WHERE Username = @Username)
BEGIN
SELECT -1 -- Username exists.
END
ELSE IF EXISTS(SELECT Id FROM Users WHERE Email = @Email)
BEGIN
SELECT -2 -- Email exists.
END
ELSE
BEGIN
INSERT INTO [Users]
([Username]
,[Password]
,[Email]
,[RegDate])
VALUES
(@Username
,@Password
,@Email
,GETDATE())
SELECT SCOPE_IDENTITY() -- UserId
END
END
Any ideas?