使用PHP 7.1,PDO,我试图对远程SQL Server数据库运行一个查询,该数据库连接良好并且能够运行查询,并且在使用用户定义的函数时可以接受。
仅通过PHP在SQL Server数据库上直接运行时,语句运行良好。
我已经通过PHP在没有用户定义函数的情况下运行了此程序,并且运行正常,我得到了结果。
任何帮助都会很棒。
谢谢
$pdo = new \PDO(
sprintf(
"dblib:host=%s;dbname=%s",
$server,
$database
),
$user,
$pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "
select
acCode AS 'Ac Code',
acCompany AS 'Company Name',
myDBName.myFunctionName(2020, 0, acCode) AS 'Balance'
from
myDBName.CUSTSUPP
where
acCustSupp = 'C'
and
myDBName.myFunctionName(2020, 0, acCode) > 0
order by
acCode";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
答案 0 :(得分:1)
UDF通过指定所有者,名称和参数在SELECT语句中执行。在您的情况下,如果myDBName
不是所有者名称,则需要使用dbo
或实际所有者进行更改。另外,您需要包括异常处理以获取实际的错误消息。
您可以根据问题中的代码尝试以下代码:
<?php
// PDO object
$server = "myserver";
$database = "myDBName";
$user = "myuser":
$pass = "mypassword";
try
$pdo = new \PDO(
sprintf(
"dblib:host=%s;dbname=%s",
$server,
$database
),
$user,
$pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die ("Error connecting. ".$e->getMessage());
}
// Statement
try
$query = "
select
acCode AS [Ac Code],
acCompany AS [Company Name],
dbo.myFunctionName(2020, 0, acCode) AS [Balance]
from
CUSTSUPP
where
acCustSupp = 'C'
and
dbo.myFunctionName(2020, 0, acCode) > 0
order by
acCode";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
// ... Additional code here
} catch (PDOException $e) {
die ("Error executing query. ".$e->getMessage());
}
// End
$statement = null;
$pdo = null;
?>