php调用MySQL存储过程的方法集合(推荐)

yipeiwu_com6年前Mysql基础
类型一:调用带输入、输出类型参数的方法
复制代码 代码如下:

$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; //输出来自存储过程中输出的变量

类型二:调用带多个输出类型和多个输入类型参数的方法
复制代码 代码如下:

$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

类型三:调用带返回结果集的方法
复制代码 代码如下:

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']; //输出数据集
 }

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

类型四:调用带返回多个结果集的方法(目前只能通过mysqli来实现~~)
复制代码 代码如下:

//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 ……
……

相关文章

php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例

php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例

本文实例讲述了php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率。分享给大家供大家参考,具体如下: <?php /** * 测试pdo和mysql...

php+mysqli实现批量执行插入、更新及删除数据的方法

本文实例讲述了php+mysqli实现批量执行插入、更新及删除数据的方法。分享给大家供大家参考。具体如下: mysqli批量执行插入/更新/删除数据,函数为 multi_query()。...

从Web查询数据库之PHP与MySQL篇

从Web查询数据库:Web数据库架构的工作原理 一个用户的浏览器发出一个HTTP请求,请求特定的Web页面,在该页面中出发form表单提交到php脚本文件(如:results.php)中...

解决PHP在DOS命令行下却无法链接MySQL的技术笔记

正好今天朋友 xjb 也碰到了这个问题,所以写了这篇笔记,将此问题的描述以及解决记录下。 问题描述:用 web 方式, 可以链接 mysql, 但是在命令行下, 却提示:   Fatal...

深入分析使用mysql_fetch_object()以对象的形式返回查询结果

mysql_fetch_object()同样用于获取查询数据结果集,返回当前行数据,并自动滑向下一行。但与mysql_fetch_row()和mysql_fetch_array()不同的...