Php method set for calling MySQL stored procedure (recommended)

Source: Internet
Author: User
This article collects and summarizes the methods for php to call the MySQL stored procedure. For more information, see Type 1: Call methods with input and output parameters

The code is as follows:


$ ReturnValue = '';
Try {
Mysql_query ("set @ Return ");
$ Spname = 'p _ Test_GetInfo1 ';
Mysql_query ("call $ spname (@ Return, '{$ userId}', '{$ pwd}')") or die ("[$ spname] Query failed :". mysql_error ());
$ Result_return = mysql_query ("select @ Return ");
$ Row_return = mysql_fetch_row ($ result_return );
$ ReturnValue = $ row_return [0];
} Catch (Exception $ e ){
Echo $ e;
}
Echo $ returnValue; // output the variables output from the stored procedure


Type 2: Call a method with multiple output types and multiple input type parameters

The code is as follows:


$ UserId = 0;
Try {
Mysql_query ("set @ Message ");
Mysql_query ("set @ Id ");
Mysql_query ("call P _ Test_Login (@ Message, @ Id, '{$ userId}', '{$ pwd}')", $ conn) or die ("Query failed :". mysql_error ());
$ Result_mess = mysql_query ("select @ Message ");
$ Result_uid = mysql_query ("select @ Id ");
$ Row_mess = mysql_fetch_row ($ result_mess );
$ Row_uid = mysql_fetch_row ($ result_uid );
$ Proc_Error = $ row_mess [0];
$ UId = $ row_uid [0];
}
Catch (Exception $ e)
{
Echo $ e;
}
Echo 'proc return message: '$ Proc_Error .'
'; // Output the variables output from the stored procedure
Echo 'user id: '. $ uId; // obtain the User id


Type 3: Call a method with a returned result set

The code is as follows:


Try {
$ Spname = 'p _ Test_GetData ';
$ Query = mysql_query ("call $ spname ()", $ conn) or die ("[$ spname] Query failed:". mysql_error ());
While ($ row = mysql_fetch_array ($ query )){
Echo $ row ['provinceid']. ':'. $ row ['provincename']; // output the dataset
}

} Catch (Exception $ e ){
Echo $ e;
}


Type 4: Call a method with multiple returned result sets (currently only implemented through mysqli ~~)

The code is as follows:


// PHP
$ Rows = array ();
$ Db = new mysqli ($ server, $ user, $ psd, $ dbname );
If (mysqli_connect_errno ()){
$ This-> message ('Can not connect to MySQL server ');
}
$ Db-> query ("set names UTF8 ");
$ Db-> query ("SET @ Message ");
If ($ db-> real_query ("call P _ Test_GetData2 (@ Message )")){
Do {
If ($ result = $ db-> store_result ()){
While ($ row = $ result-> fetch_assoc ()){
Array_push ($ rows, $ row );
}
$ Result-> close ();
}
} While ($ db-> next_result ());
}
$ Db-> close ();
Print_r ($ rows );
// Procedure
......
Select * from T1 where ......
Select * from T2 where ......
......

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.