php預存程序調用執行個體代碼_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
//比如要調用的預存程序為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);

執行個體四:傳出參數的inout預存程序
複製代碼 代碼如下:
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end;
";
mysql_query($sql);//建立一個myproce4的預存程序
$sql = "set @sexflag = 1";
mysql_query($sql);//設定性別參數為1
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//調用myproce4的預存程序,在cmd下面看效果

執行個體五:使用變數的預存程序
複製代碼 代碼如下:
$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);//建立一個myproce5的預存程序
$sql = "call test.myproce5(4,6);";
mysql_query($sql);//調用myproce5的預存程序,在cmd下面看效果

執行個體六:case文法

複製代碼 代碼如下:
$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select '及格';
when 80 then select '及良好';
when 100 then select '優秀';
else select '未知分數';
end case;
end;
";
mysql_query($sql);//建立一個myproce6的預存程序
$sql = "call test.myproce6(100);";
mysql_query($sql);//調用myproce6的預存程序,在cmd下面看效果

執行個體七:迴圈語句

複製代碼 代碼如下:
$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);//建立一個myproce7的預存程序
$sql = "call test.myproce7();";
mysql_query($sql);//調用myproce7的預存程序,在cmd下面看效果

執行個體八:repeat語句

複製代碼 代碼如下:
$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);//建立一個myproce8的預存程序
$sql = "call test.myproce8();";
mysql_query($sql);//調用myproce8的預存程序,在cmd下面看效果

執行個體九:loop語句

複製代碼 代碼如下:
$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);//建立一個myproce9的預存程序
$sql = "call test.myproce9();";
mysql_query($sql);//調用myproce9的預存程序,在cmd下面看效果

執行個體十:刪除預存程序

mysql_query("drop procedure if exists myproce");//刪除test的預存程序
執行個體十:預存程序中的遊標
總結中。

http://www.bkjia.com/PHPjc/326478.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326478.htmlTechArticle複製代碼 代碼如下: //比如要調用的預存程序為gxtj(a,b) $db=new mysqli("localhost","ssss","aaaaa","bbbb"); mysqli_query($db,"SET NAMES utf8"); $result=$db-query("ca...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.