Example of a stored procedure in PHP and MySQL

Source: Internet
Author: User
Tags stored procedure example

The following is an example of the stored procedure in PHP and MySQL. I saw the demo of the stored procedure in PHP and MySQL on the relevant website two days ago, let's share it with you. I hope this will help you in your future studies.

PHP and MySQL Stored Procedure Example 1: stored procedure without Parameters

 
 
  1. $ Conn = MySQL_connect ('localhost', 'root', 'root') or die ("data connection Error !!! ");
  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 );

Create a myproce Stored Procedure

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

When the stored procedure of myproce is called, a new record is added to the database.

PHP and MySQL Stored Procedure Example 2: Stored Procedure of input parameters

 
 
  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); 

Create a stored procedure for myproce2

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

Calling the stored procedure of myproce2 has no effect. You can see the result in cmd.

PHP and MySQL Stored Procedure Example 3: Stored Procedure of outgoing Parameters

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

Create a stored procedure for myproc4

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

Call the stored procedure of myproc4

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

PHP and MySQL Stored Procedure Example 4: inout Stored Procedure of outgoing Parameters

 
 
  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); 

Create a myproce4 Stored Procedure

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

Set gender parameter to 1

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

Call the stored procedure of myproce4. See the result in cmd.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.