Using stored procedures with php and mysql_PHP tutorial

WBOY
Release: 2016-07-13 10:31:02
Original
886 people have browsed it

How PHP calls mysql stored procedures, the following ten small cases serve as inspiration:

Example 1: Stored procedure without parameters

$conn = mysql_connect('localhost','root','root') or die ("connect db fail!");
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的存储过程,则数据库中将增加一条新记录。
Copy after login

Example 2: Stored procedure passing in 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下看到结果。
Copy after login

Example 3: Stored procedure of 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 '<pre class="brush:php;toolbar:false">';print_r($array);
Copy after login

Example 4: inout stored procedure of 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下面看效果
Copy after login

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下面看效果
Copy after login

Example 6: case syntax

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

Example 7: 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下面看效果
Copy after login

Example 8: 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下面看效果
Copy after login

Example 9: 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下面看效果
Copy after login

Example 10: Delete stored procedure

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

Articles you may be interested in

  • mysql creates a stored procedure and calls it in php
  • Use the PHP function memory_get_usage to get the current PHP memory consumption to implement the program Performance Optimization
  • Several solutions to UFT-8 Chinese encoding garbled characters in PHP and MySQL
  • How PHP determines whether a string is all in English, pure Chinese, or a combination of Chinese and English
  • Mysql database cache function analysis, debugging and performance summary
  • Interconversion of dates and unix timestamps in php and mysql
  • mysql query today, yesterday, last 7 days, last 30 days, Methods for this month and previous month’s data
  • Methods for PHP asynchronous debugging and online debugging of website programs

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764158.htmlTechArticleHow to call mysql stored procedure in PHP, the following ten small cases will inspire you: Example 1: Stored procedure without parameters $conn = mysql_connect('localhost','root','root') or die ("connect db fail!...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template