php7如何执行存储过程并获取结果集

 参考连接:https://www.thinbug.com/q/49168731

连接数据库

<?php 
    $hostname = "127.0.0.1:3306"; //主机名,可以用IP代替
    $database = "experiment4"; //数据库名
    $username = "root"; //数据库用户名
    $password = ""; //数据库密码
    $conn = mysqli_connect($hostname, $username, $password, $database); 

    if(!$conn){
        echo '连接失败';
    }
    else{
        $dbase = mysqli_select_db($conn, $database);     
    }

?> 

第一种方法(多个结果集)

<?php
    include("functionsAll.php");
    include("connect.php");

    $query= "call showProducts()";

    //$conn为连接
    $sqlCall= mysqli_prepare($conn, $query);
    //执行
    mysqli_stmt_execute($sqlCall);
 
    //获取结果集
    while($oResult= mysqli_stmt_get_result($sqlCall)){
        if (mysqli_num_rows($oResult) > 0) 
        {
          while ($oRow = mysqli_fetch_assoc($oResult)) 
          {
            foreach($oRow as $key => $val) 
            {
              $aRows[$key] = $val;
              echo $key." ".$val." "; 
            }
            echo "<br/>";
          }  
        }
        mysqli_free_result($oResult); 
        mysqli_stmt_next_result($sqlCall);
    }
?>

第二种方法(单个结果集)

<?php
    include("functionsAll.php");
    include("connect.php");

    $query= "call showProducts()";

    $result= mysqli_query($conn, $query);
    while($res= mysqli_fetch_assoc($result)){
        foreach($res as $key=> $value){
            echo $key." ".$value." ";
        }
        echo "<br/>";
    }
?>
发布了184 篇原创文章 · 获赞 25 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weixin_41879093/article/details/103479266
今日推荐