Home Database Mysql Tutorial mysql存储过程变量使用-bug记要

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

Jun 07, 2016 pm 04:16 PM
mysql use variable storage process

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles