奇怪的 PDO MySQL 问题

发布于 2024-08-18 02:19:31 字数 1505 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-08-25 02:19:31

如果尚未启用,请尝试启用错误:

error_reporting(E_ALL);
$dbhDevices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try enabling errors if haven't done already:

error_reporting(E_ALL);
$dbhDevices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
云朵有点甜 2024-08-25 02:19:31

在旧版本的 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.

固执像三岁 2024-08-25 02:19:31

我通过使用 LEFT JOIN 而不是 INNER JOIN 使其工作。奇怪,但它正在工作。

I got it working by using a LEFT JOIN instead of an INNER JOIN. Strange, but it is working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文