Php method for calling mysql stored procedures and functions-PHP source code

Source: Internet
Author: User
Ec (2); stored procedures and functions are just introduced by MySql5.0. This operation is not directly supported in PHP. However, due to the design of mysqlphp API, we can support calling stored procedures and functions in mysqlphp API in the previous PHP version. Call stored procedures and functions in php. 1. The method that calls the stored procedure. A. If the stored procedure has an ININOUT parameter, declare a variable and input the parameter to the stored procedure. The variable is script ec (2); script

Stored Procedures and functions are just introduced by MySql5.0. This operation is not directly supported in PHP. However, due to the design of the Mysql php api, we can support calling stored procedures and functions in the mysql PHP api of the previous php version.
Call stored procedures and functions in php.
1. The method that calls the stored procedure.
A. If the stored procedure has the IN/INOUT parameter, declare a variable and input the parameter to the stored procedure. The variable is a pair,
A php variable (or not required, but there is no way to enter the php variable dynamically), a Mysql
Variable.
B. If the stored procedure has an OUT variable, declare a Mysql variable.
The declaration of mysql variables is special. the mysql server must know the existence of this variable, that is, to execute a mysql statement.
Input set @ mysqlvar = $ phpvar;
C. Use mysql_query ()/mysql_db_query () to execute the mysql variable declaration statement.
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 can be phpar input.
D.
If the stored procedure is used.
1. Execute the call procedure () statement.
That is, mysql_query ("call proceduer ([var1]...)");
2. If a return value exists, run select @ ar to return the execution result.
Mysql_query ("select @ var )"
The next operation is the same as executing a mysql statement in php. You can use functions such as mydql_fetch_row () to obtain the result.
If the time function.
You can directly execute select function.
$ Host = "localhost ";
$ User = "root ";
$ Password = "11212 ";
$ Db = "samp_db ";
$ Dblink = mysql_connect ($ host, $ user, $ password)
Or die ("can't connect to mysql ");
Mysql_select_db ($ db, $ dblink)
Or die ("can't select samp_db ");
$ Res = mysql_query ("set @ a = $ password", $ dblink );
$ Res = mysql_query ("call aa (@ a)", $ dblink );
$ Res = mysql_query ("select @ a", $ dblink );
$ Row = mysql_fetch_row ($ res );
Echo $ row [0];

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.