PHP和SQL Server存储过程执行
问题描述:
我有一个存储过程做一个登录过程,并在不同的条件下返回不同的值在相同的结构。PHP和SQL Server存储过程执行
这是我的代码:
create PROCEDURE [dbo].[I_SPCheckUserLogin]
@username nvarchar(50),
@password varchar(max)
AS
Begin
declare @incorrect_pass_msg nvarchar(MAX) = N'کاربر گرامی کلمه عبور نادرست است';
declare @login_success_pass_msg nvarchar(MAX) = N'کاربر گرامی عملیات ورود با موفقیت انجام شد';
declare @user_not_exists_msg nvarchar(MAX) = N'چنین کاربری یافت نشد';
declare @lock_user_msg nvarchar(MAX) = N'کاربر گرامی به دلایل امنیتی شما قادر به ورود نمی باشید';
declare @change_pass_msg nvarchar(MAX) = N'به دلایل امنیتی باید کلمه عبور تغییر یابد';
declare @error_number int = 0;
declare @msg nvarchar(100);
declare @version nvarchar(50);
declare @rule_lock_login_time int;
declare @rule_attempt_login int;
declare @rule_expire_days int;
select @version = ValueI from I_Rules where RuleName = 'Version';
select @rule_lock_login_time = ValueI from I_Rules where RuleName = 'LockLoginTime';
select @rule_attempt_login = ValueI from I_Rules where RuleName = 'AttemptToLogin';
select @rule_expire_days = ValueI from I_Rules where RuleName = 'PasswordExpireDays';
declare @md5_number int;
select top 1 @md5_number = Value from SplitString(@version,'.') order by Id desc;
declare @m_password nvarchar(MAX);
set @m_password = (select dbo.Md5Generator(@password , @md5_number));
declare @uid int ,
@uname nvarchar(max),
@upass nvarchar(max),
@ulast_login_date nvarchar(10),
@ulast_login_time nvarchar(10),
@ulock_login_time nvarchar(10),
@umust_change_pass bit,
@upass_never_expire bit,
@uattempt_login int,
@ulast_change_pass_date nvarchar(10);
select @uid = Id,
@uname = Username,
@upass = [Password],
@ulast_login_date = LastLoginDate,
@ulast_login_time = LastLoginTime,
@ulock_login_time = LockLoginTime,
@umust_change_pass = UserMustChangePassword,
@upass_never_expire = PasswordNeverExpire,
@uattempt_login = AttemptToLogin,
@ulast_change_pass_date = LastChangePasswordDate
from I_Users
where Username = @username and
IsActive='true';
declare @server_time nvarchar(10),
@server_date nvarchar(10);
select @server_date = [Date],
@server_time = [Time]
from dbo.GetShamsiDateTime();
if(@uname is null)
begin
set @error_number = 1;
set @msg = @user_not_exists_msg;
select @error_number error , @msg [message];
return;
end
else
begin
if(@ulock_login_time is not null)
begin
if(@server_time <= (select dbo.AddMinuteToTime(@ulock_login_time, @rule_lock_login_time)))
begin
set @error_number = 1;
set @msg = @lock_user_msg;
select @error_number error , @msg [message];
update I_Users Set [email protected]_time Where [email protected];
return;
end
else
begin
update I_Users
set LastLoginDate = @server_date,
LastLoginTime = @server_time,
LockLoginTime = null,
AttemptToLogin = 0
where Id = @uid;
if(@@ERROR !=0)
begin
select 3 error , 'update faild' [message];
return;
end
end
end
if(@upass != @m_password)
begin
declare @tmp table(uattemp_login nvarchar(max));
update I_Users
set AttemptToLogin = AttemptToLogin + 1
output inserted.AttemptToLogin into @tmp
where Id = @uid;
if(@@ERROR !=0)
begin
select 3 error , 'update faild' [message];
return;
end
set @uattempt_login = (select uattemp_login from @tmp);
if @uattempt_login = @rule_attempt_login
begin
update I_Users set LockLoginTime = @server_time where [email protected]
if(@@ERROR !=0)
begin
select 3 error , 'update faild' [message];
return;
end
end
set @error_number = 1;
set @msg = @incorrect_pass_msg;
select @error_number error , @msg [message];
return;
end
else
begin
if @umust_change_pass = 'true'
begin
set @error_number = 2;
set @msg = @change_pass_msg;
select @error_number error , @msg [message];
return;
end
else
begin
if @upass_never_expire = 'false'
begin
if @server_date> (select [date] from AddDaysToDate_Custom((select dbo.ShamsitoMiladi(@ulast_change_pass_date)),@rule_expire_days))
begin
set @error_number = 2;
set @msg = @change_pass_msg;
select @error_number error , @msg [message];
return;
end
end
update I_Users
set LastLoginDate = @server_date,
LastLoginTime = @server_time,
LockLoginTime = null,
AttemptToLogin = 0
where Id = @uid;
if(@@ERROR !=0)
begin
select 3 error , 'update faild' [message];
return;
end
set @error_number = 0;
set @msg = @login_success_pass_msg;
select @error_number error , @msg [message];
return;
end
end
end
end
的问题是,当执行这样这个存储过程:
$username = trim($username);
$password = trim($password);
$dbconn = new db_connection();
$select_statement = "execute I_SPCheckUserLogin '{$username}','{$password}'";
echo $select_statement;
$result = $dbconn->do_sql_command($select_statement);
$count = 0;
$row = $dbconn->fetch_array($result);
if ($row['error'] == '0') {
echo '<br/>';
$this->load->library('session');
$ci =& get_instance();
$data = array();
$data['username'] = $username;
$data['password'] = $password;
$data['system_id'] = $system_id;
$ci->session->set_userdata('user_info', $data);
redirect('control_panel/index');
} else
echo $row['message'];
大量测试后的问题是执行的更新语句时这个存储过程其余的存储过程不是从执行php 而是在sql server管理工作室执行像魅力
对不起我的英语
答
耽搁OK抱歉,但我找到了解决办法
我一定要把这是命令:SET NOCOUNT ON
SET NOCOUNT ON加入是为了避免额外的结果集 干扰用SELECT语句。
它解决了我的问题
你在存储过程中完成整个登录过程的原因是什么? – CodeZombie 2013-05-15 13:37:57
这不仅仅是一个登录过程,我会做更多的事情,但我想要做大部分存储过程的工作,因为这个存储过程将被多次使用,我以这种方式看到它更好 – JUSPER 2013-06-10 10:18:52