简体   繁体   中英

How to call a SQL Server stored procedure using pdo connection in php?

I have two stored procedures getAllUser and getAllEvent() in a SQL Server database. I can successfully connect to the database using the following code:

try {
    $hostname = "xxxxxx";
    $port = xxxx;
    $dbname = "xxxxx";
    $username = "xxxxx";
    $pw = "xxxxxx";
    $dbh = new PDO ("sqlite:server=$hostname;Database=$dbname","$username","$pw");
    echo "connected";
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }

Now how I will call those stored procedure ....

  • getAllUser doesn't require any parameter
  • getAllEvent requires three parameters @FromDate , @ToDate and @UserID

How to do this? Please help

You need to prepare and execute a PDO statement using PDO::query() or PDO::prepare() and PDOStatement::execute() .

Bind params using PDOStatement::bindParam() .

Call procedure using CALL syntax.

If your stored procedure returns resul set, fetch data using PDOStatement::fetch() or PDOStatement::fetchAll() :

Next is working example using PHP 7.1.12, PHP Driver for SQL Server 4.3.0+9904, SQL Server 2012 Express Edition. Both {CALL sp_name} and EXEC sp_name work.

T-SQL:

CREATE PROCEDURE [dbo].[getAllUser]
AS BEGIN
    SELECT 'Result from stored procedure.' AS [ResultText]
END

CREATE PROCEDURE [dbo].[getAllEvent]
     @FromDate date, 
     @ToDate date,
     @UserID int
AS 
BEGIN
    SELECT 
        @FromDate AS FromDate,
        @ToDate AS ToDate,
        @UserID AS UserID
END

PHP:

<?php
# Connection info
$hostname = 'server\instance,port';
$dbname   = 'database';
$username = 'username';
$pw       = 'password';

# Connection
try {
    $dbh = new PDO("sqlsrv:server=$hostname;Database=$dbname", $username, $pw);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
    die( "Error connecting to SQL Server. ".$e->getMessage());
}

# First procedure
try {
    echo "Procedure: getAllUser"."<br>";
    $sql = "{CALL getAllUser}";
    #$sql = "EXEC getAllUser";
    $stmt = $dbh->query($sql);
    # If your stored procedure returns information about records affected, fetch next result set
    $stmt->nextRowset();  
    while ($row = $stmt->fetch( PDO::FETCH_ASSOC )) {
       foreach($row as $name => $value) {
          echo $name.": ".$value."<br>";
       }    
    }
    echo "<br>";
} catch( PDOException $e ) {
    die( "Error executing stored procedure: ".$e->getMessage());
}
$stmt = null;

# Second procedure
try {
    echo "Procedure: getAllEvent"."<br>";
    $sql = "{CALL getAllEvent(?, ?, ?)}";
    #$sql = "exec getAllEvent ?, ?, ?";
    $fromDate = '2008-08-03'; 
    $toDate = '2008-08-05';
    $userID = 123;
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(1, $fromDate);
    $stmt->bindParam(2, $toDate);
    $stmt->bindParam(3, $userID);
    $stmt->execute();
    while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
        foreach($row as $name => $value) {
            echo $name.": ".$value."<br>";
        }   
    }
    echo "<br>";
} catch( PDOException $e ) {
    die( "Error connecting to SQL Server" );
}
$stmt = null;

# End
$dbh = null;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM