hedemann
hedemann

Reputation: 21

PHP PDO MSSQL SP - no return values

I need some help in relation to PHP PDO MSSQL Stored Procedure.

I have a Stored Procedure which is called with two parameters userId and pwd, the Stored Procedure then returns two values status and token (using SELECT @status as status, null as token in the Stored Procedure to return the value)

When I try to call the Stored Procedure from PHP (ver. 7.0) using PDO I don't receive any return values

This is the PHP code:

$conn = new PDO("sqlsrv:server=".$host.";Database=".$db_name, 
$username,$password);

$userId = "2465";   
$pwd = "460";

$query = "exec sp_getToken @userId=:userId, @pwd=:pwd";
$stmt = $conn->prepare($query);

$stmt->bindValue(':userId', $userId);
$stmt->bindValue(':pwd', $pwd);

$stmt->execute();

while($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    var_dump($result);
}

Can anyone tell what to do?

Upvotes: 0

Views: 1270

Answers (3)

hedemann
hedemann

Reputation: 21

Problem solves :)

By adding "SET NOCOUNT ON" to my stored procedure. Obviously the problem was related to the facts, that a stored procedure returns two results, the first result containing the number of rows affected and the second result containing the actual data.

Thanks to everybody for trying helping me :)

Upvotes: 1

Zhorov
Zhorov

Reputation: 29943

It is almost the same, but you may try with this:

<?php
$host = 'server\instance,port';
$db_name = 'database';
$username = 'user';
$password = 'password';

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

# Stored procedure
try {
    $query = "{call sp_getToken(@userId=?, @pwd=?)}";
    $userId = "2465";   
    $pwd = "460";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(1, $userId, PDO::PARAM_STR);
    $stmt->bindParam(2, $pwd, PDO::PARAM_STR);
    $stmt->execute();
    while($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        var_dump($result);
        echo"</br>";
    }
} catch(PDOException $e) {
    die("Error executing stored procedure: ".$e->getMessage());
}
$stmt = null;

#
$conn = null;
?>

Upvotes: 1

Tschallacka
Tschallacka

Reputation: 28722

Stored procedures are stored in the database schema I believe.

If you add the schema to your query SQL server should know where to "look" for your stored procedure.

$query = "EXEC [dbo].[sp_getToken] @userId=:userId, @pwd=:pwd";

Also when binding the parameters it might help defining the type. I've had issues with SQL server where defining the parameter typed resolved the issue.

$stmt->bindValue(':userId', $userId, PDO::PARAM_STR);
$stmt->bindValue(':pwd', $pwd, PDO::PARAM_STR);

Also, make sure that the user that PHP logs into the database has the Execute permission in SQL Server.

https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/grant-permissions-on-a-stored-procedure?view=sql-server-2017

In Object Explorer, connect to an instance of Database Engine and then expand that instance.

Expand Databases, expand the database in which the procedure belongs, and then expand Programmability.

Expand Stored Procedures, right-click the procedure to grant permissions on, and then click Properties.

From Stored Procedure Properties, select the Permissions page.

To grant permissions to a user, database role, or application role, click Search.

In Select Users or Roles, click Object Types to add or clear the users and roles you want.

Click Browse to display the list of users or roles. Select the users or roles to whom permissions should be granted.

In the Explicit Permissions grid, select the permissions to grant to the specified user or role. For a description of the permissions, see Permissions (Database Engine).

Selecting Grant indicates the grantee will be given the specified permission. Selecting Grant With indicates that the grantee will also be able to grant the specified permission to other principals.

Upvotes: 0

Related Questions