搜索
首页数据库mysql教程mysql存储过程变量使用-bug记要

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,改完后,再写了段代码测了一遍,没问题了
让测试测了几遍,也没问题了,好了,基本确定问题在哪里了,下面就知道问题出在哪里了,继续优化。。。
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL字符串类型:存储,性能和最佳实践MySQL字符串类型:存储,性能和最佳实践May 10, 2025 am 12:02 AM

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)长度,始终使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

了解MySQL字符串类型:VARCHAR,文本,char等了解MySQL字符串类型:VARCHAR,文本,char等May 10, 2025 am 12:02 AM

mySqlStringTypesIncludeVarChar,文本,char,enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptOptoPeptoPepecifientlimit.2)textisidealforlargetStortStorStoverStorextorewiteWithoutAdefinedLengthl.3)charlisfixed-Length

MySQL中的字符串数据类型是什么?MySQL中的字符串数据类型是什么?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

如何向新的MySQL用户授予权限如何向新的MySQL用户授予权限May 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

如何在MySQL中添加用户:逐步指南如何在MySQL中添加用户:逐步指南May 09, 2025 am 12:14 AM

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

mysql:添加具有复杂权限的新用户mysql:添加具有复杂权限的新用户May 09, 2025 am 12:09 AM

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

mysql:字符串数据类型和coltrationsmysql:字符串数据类型和coltrationsMay 09, 2025 am 12:08 AM

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

MySQL:我应该在Varchars上使用什么长度?MySQL:我应该在Varchars上使用什么长度?May 09, 2025 am 12:06 AM

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具