How php calls the mysql Stored Procedure

Source: Internet
Author: User

The php Method for calling the mysql stored procedure has been found online for a long time and I have not found a good method for calling the mysql stored procedure in php. I will write a simple example about calling the mysql Stored Procedure instance by myself, I can't speak well. If you have any suggestions, you can write it yourself.

Php tutorial how to call the stored procedure of mysql tutorial
After finding a good method for calling the mysql stored procedure using php for a long time on the Internet, I will write a simple example about calling the mysql Stored Procedure instance by myself, if you have any comments, write them by yourself.
*/

Delimiter //

Create procedure pro_reg (in nc varchar (50), in pwd varchar (50), in email varchar (50), in address varchar (50 ))

Begin

Insert into tb_reg (name, pwd, email, address) values (nc, pwd, email, address );

End;


Call the Stored Procedure pro_reg to save the registration information entered by the user to the database tutorial. The Code is as follows:

If ($ SQL = $ conn-> query ("call pro_reg ('". $ nc. "','". $ pwd. "','". $ email. "','". $ address. "')"))

{

Echo "<script> alert ('user registration successful! '); </Script> ";

}

Else

{

Echo "<script> alert ('user registration failed! '); </Script> ";

}

 

$ Link = mysqli_connect ('localhost', 'root', '000000', 'test') or die ('cant connect to localhost .');

$ Query_id = mysqli_query ($ link, 'Call myselect ();');
While ($ rs = mysqli_fetch_array ($ query_id ))
{
Echo $ rs ['a']. '<br/> ';
}
Mysqli_close ($ link );


// Method 2


$ 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];

/*
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 (also can not be used, but there is no way to dynamically input php variables), 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 of phpar can be passed in.

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 subsequent operations are the same as the general mysql statement executed by php. You can use functions such as mydql_fetch_row () to obtain the result.

If the time function. You can directly execute select function.

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.