PHP调用sqlserver存储过程并获取返回值

1.获取连接

public function getConn($dbHost, $dbName, $dbUser, $dbPass) {
  $dsn = 'sqlsrv:server='.$dbHost.';Database='.$dbName;
  $conn= new PDO($dsn, $dbUser, $dbPass);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  return $conn;  
}

 

2.执行存储过程,并获取返回值

public function runStoredProcedure($dbHost, $dbName, $dbUser, $dbPass){
    $conn = getConn($dbHost, $dbName, $dbUser, $dbPass);
    $storedProcedureName = $runner->m_StoredProcedureName;
    $query = "{:retval = CALL $storedProcedureName()}";
    $stmt = $conn->prepare($query);
    $stmt->bindParam('retval', $retval, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 4);
    $stmt->execute();
    return $retval;
}


 


 

你可能感兴趣的:(php)