当我在SQL Server Management Studio中执行此行 EXEC pl.spGetInfo' JOHN DOE' 时,它运行正常。
当我调用这个php文件时,我收到一条错误消息' 警告:json_decode()期望参数1为字符串,.. '我不能'找到问题。
<?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.\n";
die(json_decode(array('outcome' => false, 'message' => 'Unable to connect')));
echo "Connection failed: " . $ex->getMessage();
// Free the statement and connection resources
$stmt->closeCursor();
}
}
?>
此致 埃利奥·费尔南德斯
答案 0 :(得分:1)
出于某种原因,似乎您的PHP代码在catch分支中运行并命中ROUND
die(json_decode(array('outcome' => false, 'message' => 'Unable to connect')));
需要一个字符串并返回一个数组(大多数情况下),但你想要反过来。因此,请使用json_decode
。