首页 > 数据库 > mysql教程 > 基于Php mysql存储过程的详解_MySQL

基于Php mysql存储过程的详解_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-01 13:24:32
原创
795 人浏览过

bitsCN.com 实例一:无参的存储过程

$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end;
";
mysql_query($sql);//创建一个myproce的存储过程

$sql = "call test.myproce();";
mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。

实例二:传入参数的存储过程

$sql = "
create procedure myproce2(in score int)
begin
if score >= 60 then
select 'pass';
else
select 'no';
end if;
end;
";
mysql_query($sql);//创建一个myproce2的存储过程
$sql = "call test.myproce2(70);";
mysql_query($sql);//调用myproce2的存储过程,看不到效果,可以在cmd下看到结果。

实例三:传出参数的存储过程

$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end;
";
mysql_query($sql);//创建一个myproce3的存储过程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//调用myproce3的存储过程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '

';print_r($array);<br><br><strong>实例四:传出参数的inout存储过程<br></strong><br>$sql = "<br>create procedure myproce4(inout sexflag int)<br>begin<br>SELECT * FROM user WHERE sex = sexflag;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce4的存储过程<br>$sql = "set @sexflag = 1";<br>mysql_query($sql);//设置性别参数为1<br>$sql = "call test.myproce4(@sexflag);";<br>mysql_query($sql);//调用myproce4的存储过程,在cmd下面看效果<br><br><strong>实例五:使用变量的存储过程<br></strong><br>$sql = "<br>create procedure myproce5(in a int,in b int)<br>begin<br>declare s int default 0;<br>set s=a+b;<br>select s;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce5的存储过程<br>$sql = "call test.myproce5(4,6);";<br>mysql_query($sql);//调用myproce5的存储过程,在cmd下面看效果<br><br><strong>实例六:case语法<br></strong><br>$sql = "<br>create procedure myproce6(in score int)<br>begin<br>case score<br>when 60 then select '及格';<br>when 80 then select '及良好';<br>when 100 then select '优秀';<br>else select '未知分数';<br>end case;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce6的存储过程<br>$sql = "call test.myproce6(100);";<br>mysql_query($sql);//调用myproce6的存储过程,在cmd下面看效果<br><br><strong>实例七:循环语句<br></strong><br>$sql = "<br>create procedure myproce7()<br>begin<br>declare i int default 0;<br>declare j int default 0;<br>while iset j=j+i;<br>set i=i+1;<br>end while;<br>select j;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce7的存储过程<br>$sql = "call test.myproce7();";<br>mysql_query($sql);//调用myproce7的存储过程,在cmd下面看效果<br><br><strong>实例八:repeat语句<br></strong><br>$sql = "<br>create procedure myproce8()<br>begin<br>declare i int default 0;<br>declare j int default 0;<br>repeat<br>set j=j+i;<br>set i=i+1;<br>until j>=10<br>end repeat;<br>select j;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce8的存储过程<br>$sql = "call test.myproce8();";<br>mysql_query($sql);//调用myproce8的存储过程,在cmd下面看效果<br><br><strong>实例九:loop语句<br></strong><br>$sql = "<br>create procedure myproce9()<br>begin<br>declare i int default 0;<br>declare s int default 0;<br><br>loop_label:loop<br>set s=s+i;<br>set i=i+1;<br>if i>=5 then<br>leave loop_label;<br>end if;<br>end loop;<br>select s;<br>end;<br>";<br>mysql_query($sql);//创建一个myproce9的存储过程<br>$sql = "call test.myproce9();";<br>mysql_query($sql);//调用myproce9的存储过程,在cmd下面看效果<br><br><strong>实例十:删除存储过程<br></strong>mysql_query("drop procedure if exists myproce");//删除test的存储过程<br><strong>实例十一:存储过程中的游标<br><font style="COLOR: #ff0000">总结:</font></strong>1.存储过程可用于InnoDB或MyISAM类型的表<br>2.show procedure status显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等<br><br>3.SHOW CREATE PROCEDURE myproce显示某一个存储过程的详细信息<br><br>bitsCN.com
    
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板