php調(diào)用MySQL存儲過程的方法集合(推薦)
更新時間:2013年07月03日 11:46:26 作者:
本篇文章是對php調(diào)用MySQL存儲過程的方法進行了集合與匯總,需要的朋友參考下
類型一:調(diào)用帶輸入、輸出類型參數(shù)的方法
$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; //輸出來自存儲過程中輸出的變量
類型二:調(diào)用帶多個輸出類型和多個輸入類型參數(shù)的方法
$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/>'; //輸出來自存儲過程中輸出的變量
echo 'User id:'.$uId; //獲取用戶id
類型三:調(diào)用帶返回結(jié)果集的方法
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']; //輸出數(shù)據(jù)集
}
} catch ( Exception $e ) {
echo $e;
}
類型四:調(diào)用帶返回多個結(jié)果集的方法(目前只能通過mysqli來實現(xiàn)~~)
//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 ……
……
復(fù)制代碼 代碼如下:
$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; //輸出來自存儲過程中輸出的變量
類型二:調(diào)用帶多個輸出類型和多個輸入類型參數(shù)的方法
復(fù)制代碼 代碼如下:
$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/>'; //輸出來自存儲過程中輸出的變量
echo 'User id:'.$uId; //獲取用戶id
類型三:調(diào)用帶返回結(jié)果集的方法
復(fù)制代碼 代碼如下:
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']; //輸出數(shù)據(jù)集
}
} catch ( Exception $e ) {
echo $e;
}
類型四:調(diào)用帶返回多個結(jié)果集的方法(目前只能通過mysqli來實現(xiàn)~~)
復(fù)制代碼 代碼如下:
//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 ……
……
相關(guān)文章
php中將地址生成迅雷快車旋風(fēng)鏈接的代碼[測試通過]
php中將地址生成迅雷快車旋風(fēng)鏈接的代碼,非常不錯,將下面的代碼保存為cs.php運行即可。2011-04-04php中在PDO中使用事務(wù)(Transaction)
事務(wù) (Transaction) 是操作數(shù)據(jù)庫中很重要的一個功能, 它可以讓你預(yù)定一條, 或者一系列 SQL 語句, 然后一起執(zhí)行2011-05-05Linux平臺PHP5.4設(shè)置FPM線程數(shù)量的方法
這篇文章主要介紹了Linux平臺PHP5.4設(shè)置FPM線程數(shù)量的方法,較為詳細的分析了Linux平臺php5.4設(shè)置FPM的相關(guān)參數(shù)、功能及使用技巧,需要的朋友可以參考下2016-11-11支持數(shù)組的ADDSLASHES的php函數(shù)
支持數(shù)組的ADDSLASHES2010-02-02