<BR>//比如要调用的存储过程为gxtj(a,b) <BR>$db=new mysqli("localhost","ssss","aaaaa","bbbb"); <BR>mysqli_query($db,"SET NAMES utf8"); <BR>$result=$db->query("call gxtj($year,$jd)"); // gxtj是mysql的存储过程名称 [color=gray][/color] <BR>while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成从返回结果集中取出一行 <BR>{ <BR>while ($key=key($row)){ //依次取得字段名 <BR>$value=current($row); //依次取得字段值 <BR>} <BR>} <BR>
实例一:无参的存储过程
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");<BR>mysql_select_db('test',$conn);<BR>$sql = "<BR>create procedure myproce()<BR>begin<BR>INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');<BR>end; <BR>";<BR>mysql_query($sql);//创建一个myproce的存储过程</P><P>$sql = "call test.myproce();";<BR>mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。<BR>
实例二:传入参数的存储过程
$sql = "<BR>create procedure myproce2(in score int)<BR>begin<BR>if score >= 60 then<BR>select 'pass';<BR>else<BR>select 'no';<BR>end if;<BR>end; <BR>";<BR>mysql_query($sql);//创建一个myproce2的存储过程<BR>$sql = "call test.myproce2(70);";<BR>mysql_query($sql);//调用myproce2的存储过程,看不到效果,可以在cmd下看到结果。<BR>
实例三:传出参数的存储过程
$sql = "<BR>create procedure myproce3(out score int)<BR>begin<BR>set score=100;<BR>end; <BR>";<BR>mysql_query($sql);//创建一个myproce3的存储过程<BR>$sql = "call test.myproce3(@score);";<BR>mysql_query($sql);//调用myproce3的存储过程<BR>$result = mysql_query('select @score;');<BR>$array = mysql_fetch_array($result);<BR>echo '<pre class="prettyprint linenums">';print_r($array);<BR>
实例四:传出参数的inout存储过程
$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>
实例五:使用变量的存储过程
$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>
实例六:case语法
$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>
实例七:循环语句
$sql = "<BR>create procedure myproce7()<BR>begin<BR>declare i int default 0;<BR>declare j int default 0;<BR>while i<10 do<BR>set 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>
实例八:repeat语句
$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>
实例九:loop语句
$sql = "<BR>create procedure myproce9()<BR>begin<BR>declare i int default 0;<BR>declare s int default 0;</P><P>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的<p style="color:transparent">。本文来源gao!%daima.com搞$代*!码网1</p><cite>搞代gaodaima码</cite>存储过程,在cmd下面看效果<BR>
实例十:删除存储过程
mysql_query(“drop procedure if exists myproce”);//删除test的存储过程
实例十:存储过程中的游标
总结中。