mysqlprocedure(mysql 存储过程怎么调用)
本文目录
mysql 存储过程怎么调用
php调用mysql存储过程和函数的方法 存储过程和函数是MySql5.0刚刚引入的。关于这方面的操作在PHP里面没有直接的支持。但是由于Mysql PHP API的设计,使得我们可以在以前的PHP版本中的mysql php api中支持存储过程和函数的调用。 在php中调用存储过程和函数。 1。调用存储过程的方法。 a。如果存储过程有 IN/INOUT参数,声明一个变量,输入参数给存储过程,该变量是一对, 一个php变量(也可以不必,只是没有php变量时,没有办法进行动态输入),一个Mysql 变量。 b。如果存储过程有OUT变量,声明一个Mysql变量。 mysql变量的声明比较特殊,必须让mysql服务器知道此变量的存在,其实也就是执行一条mysql语句。 入set @mysqlvar=$phpvar ; c。使用mysql_query()/mysql_db_query()执行mysql 变量声明语句。 mysql_query("set @mysqlvar=$pbpvar"); 这样,在mysql服务器里面就有一个变量,@mysqlar。如果是IN参数,那么其值可以由phpar传入。 d。 如果是存储过程。 1。执行 call procedure()语句。 也就是mysql_query("call proceduer(...)"); 2. 如果有返回值,执行select @ar,返回执行结果。 mysql_query("select @var)" 接下来的操作就和php执行一般的mysql语句一样了。可以通过mydql_fetch_row()等函数获得结果。 如果时函数。 直接执行 select function() 就可以了。 $host="localhost"; $user="root"; $password="11212"; $db="samp_db"; $dblink=mysql_connect($host,$user,$password) or die("can’t connect to mysql"); mysql_select_db($db,$dblink) or die("can’t select samp_db"); $res=mysql_query("set @a=$password",$dblink); $res=mysql_query("call aa(@a)",$dblink); $res=mysql_query("select @a",$dblink); $row=mysql_fetch_row($res); echo $row;
mysql 存储过程
你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难 有看不懂的地方一起讨论 :begindeclare tikk datetime ; declare done int default 0; declare userid int default 0; declare moduleid int default 0; declare couid int default 0;declare mduname varchar(255) ; declare opsid int default 0; declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status《》0 group by I_userID,I_operationID order by createtime desc;declare continue handler for sqlstate ’02000’ set done =1;set tikk = now();open c1;repeat fetch c1 into userid, opsid; if not done then select I_moduleID from space_operation where status《》0 and ID=opsid into moduleid; if moduleid 《》 ’’ then select Nvc_identification from space_operation where status《》0 and ID=opsid into @identiftion; if moduleid 》 0 then Select Nvc_ename from space_module where status《》0 and ID=moduleid into mduname; else set mduname = ’space’; end if; create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime); INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status《》0 and I_operationID=opsid and I_userID=userid ; select count(*) from sp_tab1 into couid; set @ihod = 0; set @listp = ’’; set @listpp = ’’; set @content0p = ’’; set @content0 = ’’; while couid 》 0 do select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID 》 @ihod order by ID asc limit 0,1; if @iok 《》 ’’ then if mduname = ’blog’ then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk); elseif mduname = ’team’ then if(@identiftion = ’addblog’ || @identiftion = ’mdyblog’) then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk); else set @listpp = CONCAT(@listpp,CONCAT(@objiplk,’,’)); set @operarry1p = substring_index(@conuiy,’|’,1); set @operarry2p = substring_index(@conuiy,’|’,-1); set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1))); set @objlistp = substring(@listpp,1,length(@listpp)-1); end if; elseif mduname = ’space’ then if(@identiftion = ’headphoto’ || @identiftion = ’status’) then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk); else set @listppr = CONCAT(@listppr,CONCAT(@objiplk,’,’)); set @operarry1pr = substring_index(@conuiy,’|’,1); set @operarry2pr = substring_index(@conuiy,’|’,-1); set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1))); set @objlistpr = substring(@listppr,1,length(@listppr)-1); end if; else set @listp = CONCAT(@listp,CONCAT(@objiplk,’,’)); set @operarry1 = substring_index(@conuiy,’|’,1); set @operarry2 = substring_index(@conuiy,’|’,-1); set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1))); set @objlist = substring(@listp,1,length(@listp)-1); end if; set @ihod = @iok; end if; set couid = couid -1; end while;if @content0 《》 ’’ then set @contentp = CONCAT(@operarry1,concat(’|’,@content0)); Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1; if @uitimej 《》 ’’ then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist); end if; end if; if @content0p 《》 ’’ then if @identiftion = ’addphoto’ then set @contentp = CONCAT(@operarry1p,CONCAT(’|’,@content0p)); else set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,’|’)); end if; Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1; if @uitimej 《》 ’’ then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids); end if; end if; if @content0pr 《》 ’’ then set @contentp = CONCAT(@operarry1p,concat(’|’,@content0pr)); Select createtime,ID into @uitimej,@IDjok from space_operation_record where status《》0 and I_operationID=opsid order by createtime desc limit 0,1; if @uitimej 《》 ’’ then INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids); end if; end if; delete from sp_tab1; end if; end if;until done end repeat;close c1;drop temporary table if exists sp_tab1 ;UPDATE space_operation_play SET status=0;UPDATE space_operation_display SET status=0;Select createtime into @ptimes from space_operation_stat where status《》0 order by createtime desc limit 0,1;if @ptimes 《》’’ then create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime); INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status《》0 and createtime=@ptimes order by D_stattime desc limit 0,30; select count(*) from sp_tab2 into @cou1id; set @uoj = 0; while @cou1id 》 0 do select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID 》 @uoj order by ID asc limit 0,1; if @io1k 《》 ’’ then INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa); set @uoj = @io1k; end if; set @cou1id = @cou1id -1; end while; drop temporary table if exists sp_tab2 ; end if;end
mysql procedure怎么用
这个是存储过程
比方说要写一个存储过程,向库里循环插入 500 条记录
存储过程如下:
delimiter $$;create procedure lucia_proc16(count int)beginDECLARE name_proc VARCHAR(20) CHARACTER SET utf8;DECLARE sex_proc VARCHAR(4) CHARACTER SET utf8;DECLARE age_proc INT(10);DECLARE class_proc VARCHAR(20) CHARACTER SET utf8;DECLARE Addr_proc VARCHAR(50) CHARACTER SET utf8;DECLARE i INT;set i = 1;set sex_proc = ’女’;set age_proc = 20;set class_proc = ’初三(2)班’;set Addr_proc = ’北京市朝阳区’;while i《count doset name_proc = CONCAT(’皮卡丘’,i);insert into students(Name,Sex,age,class,Addr)values(name_proc,sex_proc,age_proc,class_proc,Addr_proc);set i = i+1;end while;end$$;delimiter;
调用这个存储过程:
call lucia_proc16(500);
运行效果就是向库里插入了 500 条记录
mysql 怎么创建procedure
其实也很简单的,直接给你一个例子,你依照着实现 一个,然后再自己拓展就行了。mysql代码:DELIMITER $$ DROP PROCEDURE IF EXISTS insertInfo $$CREATE PROCEDURE insertInfo () BEGIN -- 插入数据表 insert into test (name) value (’admin’) ; END$$DELIMITER ;最后调用:call insertInfo();
本文相关文章:
![IBM公司的第一台磁盘存储器?IBM存储DS88700更换ssd盘](/static/images/nopic/18.jpg)
IBM公司的第一台磁盘存储器?IBM存储DS88700更换ssd盘
2023年12月15日 13:21
![神奇蜘蛛侠2(神奇蜘蛛侠2没有足够存储空间,游戏没法存档,求大神来破)](/static/images/nopic/25.jpg)
神奇蜘蛛侠2(神奇蜘蛛侠2没有足够存储空间,游戏没法存档,求大神来破)
2023年12月12日 12:15
![windows mail(windowsmail可压缩邮件存储,这可能需要几分钟是什么意思,怎么会出现)](/static/images/nopic/3.jpg)
windows mail(windowsmail可压缩邮件存储,这可能需要几分钟是什么意思,怎么会出现)
2023年12月8日 08:01
更多文章:
![三星i908e(我的三星i908e电池充满电后用不到一天是怎么回事)](/static/images/nopic/30.jpg)
三星i908e(我的三星i908e电池充满电后用不到一天是怎么回事)
2024年6月7日 04:14
![塞班3手机有哪些(推荐一款诺基亚塞班symbian ^3手机)](/static/images/nopic/4.jpg)
塞班3手机有哪些(推荐一款诺基亚塞班symbian ^3手机)
2024年3月6日 02:55
![华为荣耀20i忘记密码锁怎么开锁(那个我手机是华为荣耀20i的手机,锁屏密码忘了,现在解不开锁,有什么解决办法吗)](/static/images/nopic/26.jpg)
华为荣耀20i忘记密码锁怎么开锁(那个我手机是华为荣耀20i的手机,锁屏密码忘了,现在解不开锁,有什么解决办法吗)
2024年6月16日 10:44
![华为note8有没有红外线(华为nova8pro有红外线遥控功能吗)](/static/images/nopic/20.jpg)
华为note8有没有红外线(华为nova8pro有红外线遥控功能吗)
2024年5月15日 21:23
![oppok7手机价格和参数(oppok7优点和缺点介绍-oppok7评测)](/static/images/nopic/23.jpg)
oppok7手机价格和参数(oppok7优点和缺点介绍-oppok7评测)
2024年2月4日 06:00
![炫影发上去视频怎么看不到画面怎么看不到画面?订购爱动漫专题包-炫影视频都有哪些功能](/static/images/nopic/22.jpg)
炫影发上去视频怎么看不到画面怎么看不到画面?订购爱动漫专题包-炫影视频都有哪些功能
2023年8月17日 11:20
![魅蓝E评测:最贵的魅蓝手机带来了什么?魅蓝e外插u盘怎么安装](/static/images/nopic/25.jpg)
魅蓝E评测:最贵的魅蓝手机带来了什么?魅蓝e外插u盘怎么安装
2023年11月2日 00:05
![金立手机gn139(金立手机GN139是不是不能用联通3G网络啊)](/static/images/nopic/20.jpg)
金立手机gn139(金立手机GN139是不是不能用联通3G网络啊)
2023年10月14日 05:35
![htc c510e(htc c510e屏幕左上方出现上下箭头是什么意思)](/static/images/nopic/26.jpg)
htc c510e(htc c510e屏幕左上方出现上下箭头是什么意思)
2024年1月20日 23:10
![华为畅享10鸿蒙激活设备密码(华为手机恢复出厂设置后激活设备密码是多少)](/static/images/nopic/3.jpg)
华为畅享10鸿蒙激活设备密码(华为手机恢复出厂设置后激活设备密码是多少)
2024年5月4日 06:20
![红米pro+和pro有什么区别(红米k40 pro和红米k40 pro+具体有啥区别吗)](/static/images/nopic/9.jpg)
红米pro+和pro有什么区别(红米k40 pro和红米k40 pro+具体有啥区别吗)
2024年6月6日 07:06
![诺基亚6700s怎么充电(诺基亚6700S电池 第一次充电要充多长时间)](/static/images/nopic/25.jpg)
诺基亚6700s怎么充电(诺基亚6700S电池 第一次充电要充多长时间)
2024年3月29日 09:00