简体   繁体   中英

Calling SQL Server Stored Procedure using PHP PDO from Lumen for return value gives syntax error

I am trying to get the return value from the SqlServer stored procedure. But it is giving syntax error in my Ubuntu Server which uses FreeTDS.

SQLSTATE[HY000]: General error: 20018 Incorrect syntax near '0'. [20018] (severity 15) [(null)]

Below is my code:

$stateId = 1;
$testData = 0;
$retVal = 0;

$pdo = DB::connection(env('DBCONNECTION'))->getPdo();
$stmt = $pdo->prepare('EXEC ? = GetMyCities_sp @StateID = ?, @TestData = ?');
$stmt->bindParam(1, $retVal, \PDO::PARAM_INT,20);
$stmt->bindParam(2, $stateId, \PDO::PARAM_INT);
$stmt->bindParam(3, $testData, \PDO::PARAM_INT | \PDO::PARAM_INPUT_OUTPUT, 20);

$result_status = $stmt->execute();

$resultSet = $stmt->fetchAll(\PDO::FETCH_OBJ);
print_r($resultSet);
echo "<br />";
$stmt->nextRowset();

echo "Return value is ".$retVal;

The same works fine in my windows machine. Any idea what is wrong in the code?

IIRC, bound parameters need to be parameters, not procedure names. Can you give this a test, replacing the first ? with the stored procedure name?

$stmt = $pdo->prepare('EXEC your-proc-name = GetMyCities_sp @StateID = ?, @TestData = ?');
$stmt->bindParam(1, $stateId, \PDO::PARAM_INT);
$stmt->bindParam(2, $testData, \PDO::PARAM_INT | \PDO::PARAM_INPUT_OUTPUT, 20);

It has been a long time since I've played with PHP, but you might be able to do this if the first test works:

$stmt = $pdo->prepare('EXEC $retVal = GetMyCities_sp @StateID = ?, @TestData = ?');

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