Php method collection for calling MySQL stored procedures _php Tutorial

Source: Internet
Author: User
This article is a collection of methods for PHP to call MySQL stored procedures and summarizes the needs of the friend under reference

Type one: calling methods with input and output type parameters

Copy CodeThe 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 variables from stored procedure output


Type two: Calling methods with multiple output types and multiple input type parameters

Copy CodeThe 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 variables from stored procedure output
echo ' User ID: '. $uId; Get User ID


Type three: Call a method with a return result set

Copy CodeThe 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 data Set
}

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


Type four: Call a method with a return multiple result set (currently only implemented by MYSQLI)

Copy CodeThe 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 ...
......

http://www.bkjia.com/PHPjc/372461.html www.bkjia.com true http://www.bkjia.com/PHPjc/372461.html techarticle This article is a collection and summary of PHP call MySQL stored procedures method, the need for a friend reference under Type one: Call the method with input, output type parameters copy code code such as ...

  • 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.