如何使用MySQL中的过程验证用户登录?

时间:2012-06-29 15:08:39

标签: mysql sql stored-procedures

目前我正在验证用户登录,所以使用程序将用户名和密码与signupform表进行比较,然后找到一个记录集值> 0(成功)或< 0(失败),但我不是很好在程序概念,我尝试了我的水平最好,但我无法解决这个,请帮助我。 在此先感谢

我试过这个:

Create table signupform (username varchar(10), password varchar(10)); 
Insert into signupform values ('sampath','s96000');
Insert into signupform values ('yuvaraj','y96297');
select * from signupform;

// CREATE PROCEDURE for compare username and password
create procedure testing (@username varchar(10), @password varchar(10))
AS
BEGIN
select * from signupform where username = @username and password = @password
End;

4 个答案:

答案 0 :(得分:4)

我建议您创建功能:

create function testing(@username varchar(10), @password varchar(10))
RETURNS bit
BEGIN
   if EXISTS(select * from signupform where username = @username and password = @password)
   then
      return 1;
   else
      return 0; 
   end if;
end;

答案 1 :(得分:0)

尝试检查用户表中是否存在某些login / pwd

if EXISTS(SELECT TOP 1 * FROM signupform WHERE username = @username AND password = @password)
begin

set @res = 1;
end

    select @res as valid
    return

答案 2 :(得分:0)

您可以确保无论如何返回值,这样您就不必担心计数行

IF EXISTS(  
   SELECT *  
   FROM signupform  
   WHERE username = @username AND password = @password)  
BEGIN  
   SELECT 'Y'  
END  
ELSE  
BEGIN  
   SELECT 'N'  
END

或者做一个计数,但可能效率不高

SELECT COUNT(*)  
FROM signupform  
WHERE username = @username AND password = @password

答案 3 :(得分:-1)

试试这些SELECT语句:

SELECT UPPER(LEFT(@TypeName, 3)) into @TypeCode;
SELECT UPPER(LEFT(BranchName, 3)) into @Branch from tbl_erp_branches where BranchId=BranchIds;
-- SELECT UPPER(LEFT(AccountHeadName, 3)) into @Head from fin_erp_account_head where AccountHeadId=AccountHeadIds;
SELECT UPPER(LEFT(AccountHeadName, 3)) into @Head from fin_erp_account_head where AccountHeadId=AccountHeadIds;
SELECT UPPER(LEFT(CurrencyCode, 3)) into @Currency from tbl_erp_currency where CurrencyId=CurrencyIds;