PHP calls the MySQL stored procedure and returns the implementation program

Source: Internet
Author: User
This article describes in detail how to call and execute the mysql stored procedure in php and then return the value returned by the stored procedure. For more information, see. method of calling the stored procedure. a. If the stored procedure has the ININOUT parameter, declare a variable... this article describes in detail how to call and execute the mysql stored procedure in php and then return the value returned by the stored procedure. For more information, see.

Method of calling the stored procedure.

A. If the stored procedure has an IN/INOUT parameter, declare a variable and input the parameter to the stored procedure. the variable is a pair of php variables, which are not required, there is no way to perform Dynamic Input, a Mysql variable.

B. If there is an OUT variable in the stored procedure, declare a Mysql variable. the declaration of the mysql variable is special. you must let the mysql server know the existence of the variable, that is, execute a mysql statement, input set @ mysqlvar = $ phpvar;

C. Use mysql_query ()/mysql_db_query () to execute the mysql variable declaration statement.

The code is as follows:Mysql_query ("set @ mysqlvar [= $ pbpvar ]");

IN this way, there is a variable IN the mysql server, @ mysqlar. if the IN parameter is used, the value of phpar can be passed IN.

For example, using the mysqli function instance, we can first create a stored procedure in mysql, the code is as follows:

mysql> delimiter // mysql> CREATE PROCEDURE employee_list (OUT param1 INT)     -> BEGIN     ->   SELECT COUNT(*) INTO param1 FROM t;     -> END     -> // Query OK, 0 rows affected (0.00 sec) 

Write the following code in php:

Employee listing
 Query ("call employee_list ($ dept_id)") {print ('

Error: % d (% s) % sn ", mysqli_errno ($ dbh), mysqli_sqlstate ($ dbh), mysqli_error ($ dbh);} print ("

'.' '); // Open source code phprm.com while ($ row = $ result_set-> fetch_object () {printf (" N ", $ row-> employee_id, $ row-> surname, $ row-> firstname) ;}} else {printf ("
Employee_id Surname Firstname
% S % S % S
"); $ Dbh-> close ();}

The core code is as follows:

$ Result_set = $ dbh-> query ("call employee_list ($ dept_id )")

Here, the employee_list is our mysql stored procedure.

Address:

Reprinted at will, but please attach the article address :-)

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.