Home > Database > Mysql Tutorial > body text

mysql存储过程变量使用-bug记要

WBOY
Release: 2016-06-07 16:16:11
Original
1063 people have browsed it

mysql存储过程变量使用-bug记录 有一张用户表,字段如下, CREATE TABLE `tb_account` ( `a_uid` bigint(20) NOT NULL, `a_account` varchar(50) NOT NULL, `a_pwd` varchar(50) NOT NULL, PRIMARY KEY (`a_uid`), KEY `index_1` (`a_account`,`a_pwd`) USING

mysql存储过程变量使用-bug记录
有一张用户表,字段如下,
CREATE TABLE `tb_account` (
  `a_uid` bigint(20) NOT NULL,
  `a_account` varchar(50) NOT NULL,
  `a_pwd` varchar(50) NOT NULL,
  PRIMARY KEY (`a_uid`),
  KEY `index_1` (`a_account`,`a_pwd`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后写了一个登陆的存储过程:
CREATE PROCEDURE `sp_account_login`(in p_account varchar(50),in p_pwd varchar(100))
begin
declare p_uid bigint(20) default 0;
set @now:=unix_timestamp(now());

rett:loop
if p_account is null or p_account='' then
set p_uid:=-2;
leave rett;
end if;

if p_pwd is null or p_pwd ='' then
set p_uid:=-3;
leave rett;
end if;

if not exists (select a_uid from tb_account where a_account = p_account) then
set p_uid:=-11;
leave rett;
end if;

select @uid:= a_uid from tb_account where a_account = p_account and a_pwd = p_pwd;
if @uid is null or @uid='' or @uidset p_uid:=-12;
leave rett;
else
set p_uid := @uid;
leave rett;
end if;
end loop rett;

select p_uid,p_account,@now;
end

这个存储过程在语法上是没有问题的,但是会忽略一个问题:
问题1:因为p_account参数是字符串,在select a_uid from tb_account where a_account=p_account的时候,有时候会出现无法精确匹配的情况,可能是因为mysql执行存储过程的时候,因为没有把p_account参数作为一个字符串去处理,所以会出现串号。

然后修改存储过程为如下
CREATE PROCEDURE `sp_account_login`(in p_account varchar(50),in p_pwd varchar(100))
begin
declare p_uid bigint(20) default 0;
set @now:=unix_timestamp(now());

rett:loop
if p_account is null or p_account='' then
set p_uid:=-2;
leave rett;
end if;

if p_pwd is null or p_pwd ='' then
set p_uid:=-3;
leave rett;
end if;

if not exists (select a_uid from tb_account where a_account = p_account) then
set p_uid:=-11;
leave rett;
end if;

set @sql1=CONCAT('select @uid:=a_uid from tb_account where a_account="',p_account,'" and a_pwd="',p_pwd,'";');

prepare s1 from @sql1;
execute s1;
deallocate prepare s1;

if @uid is null or @uid='NULL' or @uid='' or @uidset p_uid:=-12;
leave rett;
else
set p_uid := @uid;
leave rett;
end if;
end loop rett;

select p_uid,p_account,@now;
end$$


这个时候,组装了一条sql语句,但是发现还是串号。继续排查问题。
发现出现串号的时候,是把上一次的账号登陆的p_uid返回了,所以断定肯定是缓存。

在该存储过程里,申明了一个局部变量p_uid,一个用户变量@uid,首先查询a_uid字段值给用户变量@uid,然后把用户变量赋值给局部变量。

局部变量每次执行存储过程的时候,mysql会生成一个新的局部变量,所以不会出现问题,
那问题肯定出现在用户变量上,用户变量对client全局有效,然后尝试修改如下:

CREATE PROCEDURE `sp_account_login`(in p_account varchar(50),in p_pwd varchar(100))
begin
declare p_uid bigint(20) default 0;
set @now:=unix_timestamp(now());

rett:loop
if p_account is null or p_account='' then
set p_uid:=-2;
leave rett;
end if;

if p_pwd is null or p_pwd ='' then
set p_uid:=-3;
leave rett;
end if;

if not exists (select a_uid from tb_account where a_account = p_account) then
set p_uid:=-11;
leave rett;
end if;

set @uid:=0;
set @sql1=CONCAT('select @uid:=a_uid from tb_account where a_account="',p_account,'" and a_pwd="',p_pwd,'";');

prepare s1 from @sql1;
execute s1;
deallocate prepare s1;

if @uid is null or @uid='NULL' or @uid='' or @uidset p_uid:=-12;
leave rett;
else
set p_uid := @uid;
leave rett;
end if;
end loop rett;

set @uid:=0;
select p_uid,p_account,@now;
set p_uid:=0;
end if;

在select @uid:=a_uid from tb_account的时候,先set @uid:=0;把上一次执行的变量先赋值为0,然后执行结束后,再赋值为0一遍,这里有点重复作用了,但是在没信心的情况下,先这么试着。

OK,改完后,再写了段代码测了一遍,没问题了
让测试测了几遍,也没问题了,好了,基本确定问题在哪里了,下面就知道问题出在哪里了,继续优化。。。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template