A collection of methods to invoke MySQL stored procedures in PHP (recommended) _php tips

Source: Internet
Author: User
Type one: calling methods with input and output type parameters
Copy Code code 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 variables from the stored procedure

type two: calling methods with multiple output types and multiple input type parameters
Copy Code code 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. ' <br/> '; Output variables from the stored procedure
echo ' User ID: '. $uId; Get User ID

type three: Calling a method with a return result set
Copy Code code 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 data Set
}

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

Type IV: Calls a method with return multiple result sets (currently only implemented through MYSQLI ~ ~)
Copy Code code 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.