PHP和MySQL存储过程的实例演示

以下的文章主要是向大家介绍的是PHP和MySQL存储过程的实例演示,我前两天在相关网站看见PHP和MySQL存储过程的实例演示的资料,觉得挺好,就拿出来供大家分享。希望在大家今后的学习中会有所帮助。

PHP与MySQL存储过程 实例一:无参的存储过程

 
 
 
  1. $conn = MySQL_connect('localhost','root','root') or die ("数据连接错误!!!");  
  2. MySQL_select_db('test',$conn);  
  3. $sql = "  
  4. create procedure myproce()  
  5. begin  
  6. INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');  
  7. end;   
  8. ";  
  9. MySQL_query($sql); 

创建一个myproce的存储过程

 
 
 
  1. $sql = "call test.myproce();";  
  2. MySQL_query($sql); 

调用myproce的存储过程,则数据库中将增加一条新记录。

PHP与MySQL存储过程 实例二:传入参数的存储过程

 
 
 
  1. $sql = "  
  2. create procedure myproce2(in score int)  
  3. begin  
  4. if score >= 60 then  
  5. select 'pass';  
  6. else  
  7. select 'no';  
  8. end if;  
  9. end;   
  10. ";  
  11. MySQL_query($sql); 

创建一个myproce2的存储过程

 
 
 
  1. $sql = "call test.myproce2(70);";  
  2. MySQL_query($sql); 

调用myproce2的存储过程,看不到效果,可以在cmd下看到结果。

PHP与MySQL存储过程 实例三:传出参数的存储过程

 
 
 
  1. $sql = "  
  2. create procedure myproce3(out score int)  
  3. begin  
  4. set score=100;  
  5. end;   
  6. ";  
  7. MySQL_query($sql); 

创建一个myproce3的存储过程

 
 
 
  1. $sql = "call test.myproce3(@score);";  
  2. MySQL_query($sql); 

调用myproce3的存储过程

 
 
 
  1. $result = MySQL_query('select @score;');  
  2. $array = MySQL_fetch_array($result);  
  3. echo '<pre>';print_r($array); 

PHP与MySQL存储过程 实例四:传出参数的inout存储过程

 
 
 
  1. $sql = "  
  2. create procedure myproce4(inout sexflag int)  
  3. begin  
  4. SELECT * FROM user WHERE sex = sexflag;  
  5. end;   
  6. ";  
  7. MySQL_query($sql); 

创建一个myproce4的存储过程

 
 
 
  1. $sql = "set @sexflag = 1";  
  2. MySQL_query($sql); 

设置性别参数为1

 
 
 
  1. $sql = "call test.myproce4(@sexflag);";  
  2. MySQL_query($sql); 

调用myproce4的存储过程,在cmd下面看效果

【编辑推荐】

  1. MySQL备份之根据表备份概述
  2. MySQL数据库性能优化的实际操作方案
  3. 对MySQL 存储过程中乱码的破解
  4. MySQL游标的使用笔记大全
  5. 对MySQL数据库小技巧概述
     
THE END