php存儲(chǔ)過(guò)程調(diào)用實(shí)例代碼
//比如要調(diào)用的存儲(chǔ)過(guò)程為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的存儲(chǔ)過(guò)程名稱(chēng) [color=gray][/color]
while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成從返回結(jié)果集中取出一行
{
while ($key=key($row)){ //依次取得字段名
$value=current($row); //依次取得字段值
}
}
實(shí)例一:無(wú)參的存儲(chǔ)過(guò)程
$conn = mysql_connect('localhost','root','root') or die ("數(shù)據(jù)連接錯(cuò)誤!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce的存儲(chǔ)過(guò)程
$sql = "call test.myproce();";
mysql_query($sql);//調(diào)用myproce的存儲(chǔ)過(guò)程,則數(shù)據(jù)庫(kù)中將增加一條新記錄。
實(shí)例二:傳入?yún)?shù)的存儲(chǔ)過(guò)程
$sql = "
create procedure myproce2(in score int)
begin
if score >= 60 then
select 'pass';
else
select 'no';
end if;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce2的存儲(chǔ)過(guò)程
$sql = "call test.myproce2(70);";
mysql_query($sql);//調(diào)用myproce2的存儲(chǔ)過(guò)程,看不到效果,可以在cmd下看到結(jié)果。
實(shí)例三:傳出參數(shù)的存儲(chǔ)過(guò)程
$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce3的存儲(chǔ)過(guò)程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//調(diào)用myproce3的存儲(chǔ)過(guò)程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '<pre>';print_r($array);
實(shí)例四:傳出參數(shù)的inout存儲(chǔ)過(guò)程
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce4的存儲(chǔ)過(guò)程
$sql = "set @sexflag = 1";
mysql_query($sql);//設(shè)置性別參數(shù)為1
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//調(diào)用myproce4的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例五:使用變量的存儲(chǔ)過(guò)程
$sql = "
create procedure myproce5(in a int,in b int)
begin
declare s int default 0;
set s=a+b;
select s;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce5的存儲(chǔ)過(guò)程
$sql = "call test.myproce5(4,6);";
mysql_query($sql);//調(diào)用myproce5的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例六:case語(yǔ)法
$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select '及格';
when 80 then select '及良好';
when 100 then select '優(yōu)秀';
else select '未知分?jǐn)?shù)';
end case;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce6的存儲(chǔ)過(guò)程
$sql = "call test.myproce6(100);";
mysql_query($sql);//調(diào)用myproce6的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例七:循環(huán)語(yǔ)句
$sql = "
create procedure myproce7()
begin
declare i int default 0;
declare j int default 0;
while i<10 do
set j=j+i;
set i=i+1;
end while;
select j;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce7的存儲(chǔ)過(guò)程
$sql = "call test.myproce7();";
mysql_query($sql);//調(diào)用myproce7的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例八:repeat語(yǔ)句
$sql = "
create procedure myproce8()
begin
declare i int default 0;
declare j int default 0;
repeat
set j=j+i;
set i=i+1;
until j>=10
end repeat;
select j;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce8的存儲(chǔ)過(guò)程
$sql = "call test.myproce8();";
mysql_query($sql);//調(diào)用myproce8的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例九:loop語(yǔ)句
$sql = "
create procedure myproce9()
begin
declare i int default 0;
declare s int default 0;
loop_label:loop
set s=s+i;
set i=i+1;
if i>=5 then
leave loop_label;
end if;
end loop;
select s;
end;
";
mysql_query($sql);//創(chuàng)建一個(gè)myproce9的存儲(chǔ)過(guò)程
$sql = "call test.myproce9();";
mysql_query($sql);//調(diào)用myproce9的存儲(chǔ)過(guò)程,在cmd下面看效果
實(shí)例十:刪除存儲(chǔ)過(guò)程
mysql_query("drop procedure if exists myproce");//刪除test的存儲(chǔ)過(guò)程
實(shí)例十:存儲(chǔ)過(guò)程中的游標(biāo)
總結(jié)中。
- PHP使用PDO調(diào)用mssql存儲(chǔ)過(guò)程的方法示例
- PHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法
- PHP PDO函數(shù)庫(kù)詳解
- PHP中PDO的錯(cuò)誤處理
- php中mysql連接方式PDO使用詳解
- php中在PDO中使用事務(wù)(Transaction)
- 基于Php mysql存儲(chǔ)過(guò)程的詳解
- php調(diào)用mysql存儲(chǔ)過(guò)程
- 用PHP調(diào)用Oracle存儲(chǔ)過(guò)程的方法
- PHP MSSQL 存儲(chǔ)過(guò)程的方法
- PHP基于PDO調(diào)用sqlserver存儲(chǔ)過(guò)程通用方法【基于Yii框架】
相關(guān)文章
php + ajax 實(shí)現(xiàn)的寫(xiě)入數(shù)據(jù)庫(kù)操作簡(jiǎn)單示例
這篇文章主要介紹了php + ajax 實(shí)現(xiàn)的寫(xiě)入數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了php + ajax 寫(xiě)入數(shù)據(jù)庫(kù)基本原理、操作技巧與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2020-05-05php通過(guò)array_merge()函數(shù)合并關(guān)聯(lián)和非關(guān)聯(lián)數(shù)組的方法
這篇文章主要介紹了php通過(guò)array_merge()函數(shù)合并關(guān)聯(lián)和非關(guān)聯(lián)數(shù)組的方法,涉及php中array_merge()函數(shù)操作數(shù)組合并的技巧,需要的朋友可以參考下2015-03-03php過(guò)濾輸入操作之htmlentities與htmlspecialchars用法分析
這篇文章主要介紹了php過(guò)濾輸入操作之htmlentities與htmlspecialchars用法,結(jié)合實(shí)例形式分析了php數(shù)據(jù)過(guò)濾操作的相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2017-02-02PHP實(shí)現(xiàn)的文件瀏覽器功能簡(jiǎn)單示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的文件瀏覽器功能,結(jié)合完整實(shí)例形式分析了php針對(duì)目錄與文件的遍歷、判斷、屬性讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09dede3.1分頁(yè)文字采集過(guò)濾規(guī)則詳說(shuō)(圖文教程)
dede3.1分頁(yè)文字采集過(guò)濾規(guī)則詳說(shuō)(圖文教程)...2007-04-04