复制代码 代码如下:
//比如要调用的存储过程为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></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code35680')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code35680"><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>实例五:使用变量的存储过程 <br></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code17926')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code17926"><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></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code44629')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code44629"><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> <p></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code64519')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code64519"><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></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code31407')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code31407"><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></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code30744')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code30744"><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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools