奇怪的 PDO MySQL 问题
我正在尝试在 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 客户端。
I am trying to perform a query in a PHP script. The query works fine in my MySQL client however it does not seem to working within my code. I am using PDO. I am thinking it might have limitations, because it seems to work fine with a less complicated query.
Here is the query:
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
Here is where I am trying to call it in my script:
$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)
{
Again, the query does work with the MySQL client.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果尚未启用,请尝试启用错误:
try enabling errors if haven't done already:
在旧版本的 PDO 使用中,当您出于某种原因声明 PDO 变量时,必须“初始化”它。尝试在通常初始化
$dbhDevices
的代码之前添加一行,看看是否有效。我对此表示怀疑,因为您说它可以很好地处理不太复杂的查询,但值得一试。In the older versions of PDO usage you have to "initialize" your PDO variable when you declare it for some reason. Try putting a line before this code that generically initializes
$dbhDevices
and see if that does the trick. I have my doubts because you say it works fine with less complicated queries but it's worth a try.我通过使用 LEFT JOIN 而不是 INNER JOIN 使其工作。奇怪,但它正在工作。
I got it working by using a LEFT JOIN instead of an INNER JOIN. Strange, but it is working.