php 时间段 mysql 存储_什么时候PHP经验MySQL存储过程

本文详细介绍了MySQL存储过程的概念、优点,并通过多个PHP调用存储过程的实例,展示了如何创建和执行存储过程。存储过程增强了SQL语言的功能,提高了运行速度,减少了网络流量,同时也提供了更高级的数据库安全机制。
摘要由CSDN通过智能技术生成

1、MySQL存储过程

数据库语言,我们经常使用的操作SQL语句必须首先编译在运行时。然后运行,存储过程(Stored Procedure)它被设置为完成一个特定的功能SQL报表设置。编译存储在数据库中的后。通过用户名和给定参数指定的存储过程(假设与参数的存储过程)调用运行它。

一个存储过程是一个可编程的函数,它在数据库中创建并保存。它能够有SQL语句和一些特殊的控制结构组成。当希望在不同的应用程序或平台上运行同样的函数,或者封装特定功能时,存储过程是很实用的。数据库中的存储过程能够看做是对编程中面向对象方法的模拟。它同意控制数据的訪问方式。

2、存储过程通常有下面长处:

(1)存储过程增强了SQL语言的功能和灵活性。

存储过程能够用流控制语句编写,有非常强的灵活性。能够完毕复杂的推断和较复杂的运算。

(2)存储过程同意标准组件是编程。

存储过程被创建后,能够在程序中被多次调用。而不必又一次编写该存储过程的SQL语句。并且数据库专业人员能够随时对存储过程进行改动。相应用程序源码毫无影响。

(3)存储过程能实现较快的运行速度。假设某一操作包括大量的Transaction-SQL代码或分别被多次运行,那么存储过程要比批处理的运行速度快非常多。由于存储过程是预编译的。

在首次运行一个存储过程时查询,优化器对其进行分析优化。而且给出终于被存储在系统表中的运行计划。而批处理的Transaction-SQL语句在每次运行时都要进行编译和优化,速度相对要慢一些。

(4)存储过程能过降低网络流量。针对同一个数据库对象的操作(如查询、改动),假设这一操作所涉及的Transaction-SQL语句被组织程存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的仅仅是该调用语句。从而大大添加了网络流量并降低了网络负载。

(5)存储过程可被作为一种安全机制来充分利用。系统管理员通过运行某一存储过程的权限进行限制,可以实现对对应的数据的訪问权限的限制。避免了非授权用户对数据的訪问,保证了数据的安全。

3、PHP调用MySQL案例

实例一:无參的存储过程

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

版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值