Home  >  Article  >  Backend Development  >  PHP stored procedure example analysis

PHP stored procedure example analysis

小云云
小云云Original
2018-03-07 09:33:322217browse

This article mainly shares with you the analysis of PHP stored procedure examples. For more knowledge about PHP storage, you can also pay attention to the PHP Chinese website. I hope it can help you.

The code is as follows:

$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); //依次取得字段值 
} 
}

Example one: stored procedure without parameters

$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的存储过程

Example two: stored procedure with incoming parameters

$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下看到结果。

Example three: Stored procedure for outgoing parameters

$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 &#39;<pre class="brush:php;toolbar:false">&#39;;print_r($array);

Example 4: Inout stored procedure for outgoing parameters

$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下面看效果

Example 5: Stored procedure using variables

$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下面看效果

Example 6 :case syntax

$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select &#39;及格&#39;;
when 80 then select &#39;及良好&#39;;
when 100 then select &#39;优秀&#39;;
else select &#39;未知分数&#39;;
end case;
end; 
";
mysql_query($sql);//创建一个myproce6的存储过程
$sql = "call test.myproce6(100);";
mysql_query($sql);//调用myproce6的存储过程,在cmd下面看效果

Example seven: loop statement

$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下面看效果

Example eight: repeat statement

$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下面看效果

Example nine: loop statement

$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下面看效果

Example ten: delete Stored procedure

mysql_query("drop procedure if exists myproce");//删除test的存储过程

Related recommendations:

php stored procedure call example

php stored routine, stored procedure advanced Learn_PHP Tutorial

Detailed introduction to the path storage path when PHP stores cookies

The above is the detailed content of PHP stored procedure example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn