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,改完后,再写了段代码测了一遍,没问题了
让测试测了几遍,也没问题了,好了,基本确定问题在哪里了,下面就知道问题出在哪里了,继续优化。。。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
