When I execute this line EXEC pl.spGetInfo 'JOHN DOE' in SQL Server Management Studio, it works just fine.
When I call this php file, I am getting an error message 'Warning: json_decode() expects parameter 1 to be string,..' and I cant' find the problem.
<?php
// BD
$conn = dbConnect();
// Assign parameter values
$name = 'JOHN DOE';
$data = getRecords($conn, $name);
function dbConnect(){
$DBSERVER = "xxx1";
$DBUSER = "xxx2";
$DBPASS = "xxx3";
$DBNAME = "xxx4";
// OBDC
try
{
$pdo = new PDO("odbc:DRIVER={SQL Server};Server={$DBSERVER};Database={$DBNAME}", $DBUSER, $DBPASS);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//die(json_encode(array('outcome' => true)));
//echo "Connected successfully";
return $pdo;
}
catch(PDOException $ex)
{
//die(json_decode(array('outcome' => false, 'message' => 'Unable to connect')));
echo "Connection failed: " . $ex->getMessage();
}
}
function getRecords($cn, $myname){
// Prepare the statement
$sql = 'EXEC pl.spGetInfo ?';
$stmt = $cn->prepare($sql);
$stmt->bindParam(1, $myname, PDO::PARAM_STR, 4000);
try{
// Execute the statement
$stmt->execute(array($myname));
// Records found
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Free the statement and connection resources
$stmt->closeCursor();
// Return
return $row;
}catch (PDOException $e) {
echo "Statement could not be executed.
";
die(json_decode(array('outcome' => false, 'message' => 'Unable to connect')));
echo "Connection failed: " . $ex->getMessage();
// Free the statement and connection resources
$stmt->closeCursor();
}
}
?>
Regards, Elio Fernandes