PHP5.6.7+连接SQL2008(2005)+使用SQLSRV读取记录集+返回参数

一. sqlsrv连接数据库

<?php

    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn){
        echo "Connection established.<br/><br/>";
        $server_info = sqlsrv_server_info($conn);
        if($server_info){
            foreach($server_info as $key => $value){
                echo $key.":".$value."<br/>";
            }
        }else{
            die(print_r(sqlsrv_errors(), true));
        }
        echo "<br/>";
        //
        $client_info = sqlsrv_client_info($conn);
        if($client_info){
            foreach($client_info as $key=>$value){
                echo $key.":".$value."<br/>";
            }
        }else{
            echo "Error in retrieving client info.<br/>";
        }
    }
    else {
        echo "Connection could not be established.\n";
        die(print_r(sqlsrv_errors(), true));
    }
    /* Close the connection. */
    sqlsrv_close($conn);
?>
二. 获取查询结果

<?php
    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }
    $sql = "select top 100 * from cti_agent;";
    $stmt = sqlsrv_query($conn, $sql, null);
    if($stmt === false){
        die(print_r(sqlsrv_errors(), true));
    }
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        print_r($row);
        print_r($row["agtid"].", ".$row["account"].", ".$row["agtname"].", ".$row["telnum"].", ".$row["pwd"].", ".$row["isMaster"]."<br/>");
    }
?>
三.调用存储过程(无参)

<?php
    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }
    $sp = "{call web_agent_list}";
    $stmt = sqlsrv_query($conn, $sp, null);
    if($stmt === false){
        die(print_r(sqlsrv_errors(), true));
    }
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        print_r($row);
        print_r($row["agtid"].", ".$row["account"].", ".$row["agtname"].", ".$row["telnum"].", ".$row["pwd"].", ".$row["isMaster"]."<br/>");
    }
?>
四.调用存储过程(入参)

<?php
    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }
    $tid = 1;
    $params = array(
        array($tid, SQLSRV_PARAM_IN)
    );
    $sp = "{call web_huifang_export1(?)}";
    $stmt = sqlsrv_query($conn, $sp, $params);
    if($stmt === false){
        die(print_r(sqlsrv_errors(), true));
    }
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        print_r($row);
        print_r($row["agtid"].", ".$row["account"].", ".$row["agtname"].", ".$row["telnum"].", ".$row["pwd"].", ".$row["isMaster"]."<br/>");
    }
?>
五.调用存储过程(出参)

<?php
    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }
    //prepare params
    $tid=1;
    $ttid=3;
    $kind=0;    //输出参数
    $params = array(
        array($tid, SQLSRV_PARAM_IN),
        array($ttid, SQLSRV_PARAM_IN),
        array($kind, SQLSRV_PARAM_OUT)
    );
    //stored procedure
    $sp = "{call web_tasktel_info(?,?,?)}";
    $stmt = sqlsrv_query($conn, $sp, $params);
    if(!stmt){
        die(print_r(sqlsrv_errors(), true));
    }
    while($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        $result = $row;
    }
    echo "结果集:";
    print_r($result);
    echo "<br/><br/>";
    sqlsrv_next_result($stmt);
    echo "输出参数:";
    print_r($kind);

    /* Release resources */
    sqlsrv_free_stmt($stmt);
    /* Close the connection. */
    sqlsrv_close($conn);
?>
六.调用存储过程(多结果集)

<?php
    $serverName="ip, port";
    $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }
    //stored procedure
    $sp = "{call xp_results_test()}";
    $stmt = sqlsrv_query($conn, $sp, null);
    if(!stmt){
        die(print_r(sqlsrv_errors(), true));
    }
    //遍历结果集
    echo "第一个结果集:<br/>";
    while($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        echo $row["account"]."<br/>";
    }
    echo "<br/>第二个结果集:<br/>";
    sqlsrv_next_result($stmt);
    while($row1=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        echo $row1["account"]."<br/>";
    }
    echo "<br/>第三个结果集:<br/>";
    sqlsrv_next_result($stmt);
    while($row1=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
        echo $row1["account"]."<br/>";
    }
    /* Release resources */
    sqlsrv_free_stmt($stmt);
    /* Close the connection. */
    sqlsrv_close($conn);
?>




  • 0
    点赞
  • 2
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

xiangxueping80

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值