当前位置:首页 > 芯闻号 > 充电吧
[导读]实例一:无参的存储过程复制代码 代码如下:$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");mysql_sel

实例一:无参的存储过程
复制代码 代码如下:
$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的存储过程
实例十一:存储过程中的游标
总结:1.存储过程可用于InnoDB或MyISAM类型的表
2.show procedure status显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等

3.SHOW CREATE PROCEDURE myproce显示某一个存储过程的详细信息
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

摘 要:“Apache+php+ MySQL”组成了一套完整的开发B/S架构的网络信息系统的工具。文中以该套工具开发产品售后服务管理系统为例,介绍了开发过程中的技术难点及解决方法。

关键字: Apache php MySQL 产品售后服务管理系统

PHP 7.4.9 版本现已发布,具体更新内容如下:Apache:修复了错误#79030(升级 apache2handler 的 php_apache_sapi_get_request_time 以返

关键字: php

如果使用美国服务器创建网站,则必须在美国服务器系统上创建环境。 今天,我将介绍美国服务器Linux系统的工作方式。

关键字: apache Linux php

近日消息,PHP 8.0将于11月发布,但当这个重要的新版本出现时,它遇到了很大的挫折,Windows将不支持它,原因未知。

关键字: php Windows 微软

2020 年 6 月 8 日,PHP 迎来了自己的 25 周岁生日。JetBrains 在博客中梳理了该语言自 1995 年诞生以来的种种历程,这种语言最初是用 C 语言编写的一组通用网关接口(C

关键字: php

                        我所写的项目是使用Maven开发,在pom.xml中添加如下必要依赖:         添加com.microsoft.sqlserver的mssql-

关键字: server sql 存储过程

function logging() { var x = new XMLHttpRequest(); x.onre

关键字: php

C++需要实现PHP端的:bin2Hex函数,PHP通过这种类型的字符串调用:pack转换成PHP能识别的2进制数据。 C++需要做的是实现一个bin2hex,其实只是把c++读取的2进制数据当成b

关键字: C语言 php

方法一: 在 php 端 header('HTTP/1.1 204 No Content '); 利用http的原理进行 方法二:利用src图片加载的特性完成请求 写一个函数,函数体内 var i

关键字: php

php与nginx整合 PHP-FPM也是一个第三方的FastCGI进程管理器,它是作为PHP的一个补丁来开发的,在安装的时候也需要和PHP源码一起编译,也就是说PHP-FPM被编译到PHP内核中,因

关键字: nginx php
关闭
关闭