我正在尝试在PHP脚本中执行查询。该查询在我的MySQL客户端中工作正常,但它似乎不能在我的代码中工作。我正在使用PDO。我认为它可能有局限性,因为它似乎与较简单的查询一起正常工作。
以下是查询:
SELECT D.status, D.createdBy, D.createDate, D.modifiedBy, D.modifiedDate,D.IPAddress, D.adminStatus, D.campus, D.buildingID, D.deviceShortName, D.distributionID, D.networkKey, D.deviceName, D.test, D.pkDevices, D.DNSRule, D.Domain, D.DNSOverride, D.noDNS, D.pkModel, DM.Model, DM.pkManufacturer, M.manufacturer FROM Devices AS D INNER JOIN DeviceModel AS DM ON D.pkModel = DM.pkModel INNER JOIN Manufacturer AS M ON DM.pkManufacturer = M.pkManufacturer WHERE D.status = '1' AND D.adminStatus = 'Active' ORDER BY D.deviceName
这是我试图在我的脚本中调用它的地方:
$dbhDevices = newPDO('mysql:host='.$_SESSION['OpsDBServer'].'.ops.tns.its.psu.edu;dbname='.$_SESSION['OpsDB'], $_SESSION['yoM'], $_SESSION['aMa']);
$sqlDevices = "SELECT D.status, D.createdBy, D.createDate, D.modifiedBy,
D.modifiedDate, D.IPAddress, D.adminStatus, D.campus, D.buildingID,
D.deviceShortName, D.distributionID, D.networkKey, D.deviceName, D.test,
D.pkDevices, D.DNSRule, D.Domain, D.DNSOverride, D.noDNS, D.pkModel, DM.Model,
DM.pkManufacturer, M.manufacturer
FROM Devices AS D
INNER JOIN DeviceModel AS DM ON D.pkModel = DM.pkModel
INNER JOIN Manufacturer AS M ON DM.pkManufacturer = M.pkManufacturer
WHERE D.status = '1' AND D.adminStatus = 'Active' ORDER BY D.deviceName";
foreach ($dbhDevices->query($sqlDevices) as $rowDevices)
{
同样,查询可以与MySQL客户端一起使用。
答案 0 :(得分:2)
尝试启用错误(如果尚未执行):
error_reporting(E_ALL);
$dbhDevices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
答案 1 :(得分:0)
在旧版本的PDO使用中,当您因某种原因声明PDO变量时,必须“初始化”它。尝试在此代码之前添加一行,通常初始化$dbhDevices
并查看是否可以解决问题。我有疑虑,因为你说它在没有复杂查询的情况下工作正常,但值得一试。
答案 2 :(得分:0)
我通过使用LEFT JOIN而不是INNER JOIN来实现它。奇怪,但它正在发挥作用。