Example demo of PHP and MySQL stored procedures

Source: Internet
Author: User
Tags array sql mysql php and php and mysql stored procedure example

The following article is mainly to introduce you to the PHP and MySQL stored procedures example demo, I two days in the relevant web site to see the PHP and MySQL stored procedures Examples of the information, feel very good, it was taken out for everyone to share. Hope that in the future you will be helpful in the study.

PHP and MySQL stored procedure instance one: no parameter stored procedure

					
       
        
         
        $conn = mysql_connect (' localhost ', ' root ', ' root ') or Die ("Data connection error!!!");  
mysql_select_db (' Test ', $conn);  
$sql = "  CREATE PROCEDURE Myproce ()  begin 
 INSERT into user (ID, username, sex) 
VALUES (NULL, ' s ', ' 0 '); 
         end;   ";  
mysql_query ($sql);
       
        

To create a Myproce stored procedure

$sql = "Call Test.myproce ();";

mysql_query ($sql);

Calls the myproce stored procedure, a new record is added to the database.

PHP and MySQL Stored procedure example two: stored procedures for incoming parameters

					
       
        
         
        $sql = "  CREATE PROCEDURE Myproce2 (in score int)  
begin  If score >=  
select ' Pass ';  else  select ' No ';  
End If;  end;   ";  
mysql_query ($sql);
       
        

To create a myproce2 stored procedure

$sql = "Call Test.myproce2 (70);"; mysql_query ($sql);

Call the Myproce2 stored procedure, see the effect, you can see the results under CMD.

PHP and MySQL stored procedure instance three: stored procedures for outgoing parameters

					
       
        
         
        $sql = "  CREATE PROCEDURE Myproce3 (out score int)  
begin  Set score=100;  end;   "; 
 mysql_query ($sql);
       
        

To create a myproce3 stored procedure

$sql = "Call Test.myproce3 (@score);"; mysql_query ($sql);

Calling Myproce3 stored Procedures

					
       
        
         
        $result = mysql_query (' select @score; ');  
$array = Mysql_fetch_array ($result); 
 Echo ' <pre> ';p rint_r ($array); 
PHP and MySQL
       
        

Stored procedure Instance four: InOut stored procedures for outgoing parameters

					
       
        
         
        $sql = "  CREATE PROCEDURE Myproce4 (inout sexflag int)  
begin  SELECT * from user 
WHERE sex = sexflag;  end;   ";  
mysql_query ($sql);
       
        

To create a Myproce4 stored procedure

$sql = "Set @sexflag = 1"; mysql_query ($sql); Set the gender parameter to 1

$sql = "Call Test.myproce4 (@sexflag);"; mysql_query ($sql);

Call the Myproce4 stored procedure, see the effect under 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.