小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

php實現(xiàn)存儲過程

 丶平上 2016-08-30

PHP調(diào)用MYSQL存儲過程實例

http://blog.csdn.net/ewing333/article/details/5906887 

http://www.cnblogs.com/kkcheng/archive/2010/03/19/1689672.html

http://hi.baidu.com/dreamontheway/item/8041f26ad5070131ad3e8346

實例一:無參的存儲過程

復(fù)制代碼
$conn = mysql_connect('localhost','root','root') or die ("數(shù)據(jù)連接錯誤!!!");
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)建一個myproce的存儲過程

$sql = "call test.myproce();";
mysql_query($sql);//調(diào)用myproce的存儲過程,則數(shù)據(jù)庫中將增加一條新記錄。
復(fù)制代碼

實例二:傳入?yún)?shù)的存儲過程

復(fù)制代碼
$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)建一個myproce2的存儲過程
$sql = "call test.myproce2(70);";
mysql_query($sql);//調(diào)用myproce2的存儲過程,看不到效果。
復(fù)制代碼

實例三:傳出參數(shù)的存儲過程

復(fù)制代碼
$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end; 
";
mysql_query($sql);//創(chuàng)建一個myproce3的存儲過程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//調(diào)用myproce3的存儲過程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '<pre>';print_r($array);
復(fù)制代碼

實例四:傳出參數(shù)的inout存儲過程

復(fù)制代碼
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end; 
";
mysql_query($sql);//創(chuàng)建一個myproce4的存儲過程
$sql = "set @sexflag = 1";
mysql_query($sql);//設(shè)置性別參數(shù)為1
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//調(diào)用myproce4的存儲過程
復(fù)制代碼

實例五:使用變量的存儲過程 

復(fù)制代碼
$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)建一個myproce5的存儲過程
$sql = "call test.myproce5(4,6);";
mysql_query($sql);//調(diào)用myproce5的存儲過程
復(fù)制代碼

實例六:case語法

復(fù)制代碼
$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 '未知分數(shù)';
end case;
end; 
";
mysql_query($sql);//創(chuàng)建一個myproce6的存儲過程
$sql = "call test.myproce6(100);";
mysql_query($sql);//調(diào)用myproce6的存儲過程
復(fù)制代碼

實例七:循環(huán)語句

復(fù)制代碼
$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)建一個myproce7的存儲過程
$sql = "call test.myproce7();";
mysql_query($sql);//調(diào)用myproce7的存儲過程
復(fù)制代碼

實例八:repeat語句

復(fù)制代碼
$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)建一個myproce8的存儲過程
$sql = "call test.myproce8();";
mysql_query($sql);//調(diào)用myproce8的存儲過程
復(fù)制代碼

實例九:loop語句

復(fù)制代碼
$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)建一個myproce9的存儲過程
$sql = "call test.myproce9();";
mysql_query($sql);//調(diào)用myproce9的存儲過程
復(fù)制代碼

實例十:刪除存儲過程 

mysql_query("drop procedure if exists myproce");//刪除test的存儲過程

 實例十:存儲過程中的游標

總結(jié)中。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多