代码如下:
//比如要调用的存储过程为gxtj(a,b)
$db=new mysqli("localhost","ssss","aaaaa","bbbb");
mysqli_query($db,"SET NAMES utf8");
$result=$db->query("call gxtj($year,$jd)"); // gxtj是mysql的存储过程名称 [color=gray][/color]
while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成从返回结果集中取出一行
{
while ($key=key($row)){ //依次取得字段名
$value=current($row); //依次取得字段值
}
}
实例一:无参的存储过程
复制代码 代码如下:
$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><p>实例四:传出参数的inout存储过程<br><u>复制代码</u> 代码如下:<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></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p><p>实例五:使用变量的存储过程 <br><u>复制代码</u> 代码如下:<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></p><p>实例六:case语法</p><p><u>复制代码</u> 代码如下:<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></p><p>实例七:循环语句</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/learn/2594" title="自学 PHP、MySQL和Apache"><img
src="https://img.php.cn/upload/webcode/000/000/000/5a2b9add93ca8711.png" alt="自学 PHP、MySQL和Apache" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/learn/2594" title="自学 PHP、MySQL和Apache">自学 PHP、MySQL和Apache</a>
<p>本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。
本书是第4版,经过了全面的更新、重写和扩展,包括PHP5.3最新改进的特性(例如,更好的错误和异常处理),MySQL的存储过程和存储引擎,Ajax技术与Web2.0以及Web应用需要注意的安全</p>
</div>
<a href="/xiazai/learn/2594" title="自学 PHP、MySQL和Apache" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div><p><u>复制代码</u> 代码如下:<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></p><p>实例八:repeat语句</p><p><u>复制代码</u> 代码如下:<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></p><p>实例九:loop语句</p><p><u>复制代码</u> 代码如下:<br>$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的存储过程,在cmd下面看效果<br></p><p>实例十:删除存储过程</p><p>mysql_query("drop procedure if exists myproce");//删除test的存储过程<br>实例十:存储过程中的游标<br>总结中。</p>










