2010-01-14 85 views
-1

我想在PHP腳本中執行查詢。該查詢在我的MySQL客戶端中工作正常,但它似乎並沒有在我的代碼中工作。我正在使用PDO。我認爲它可能有一些限制,因爲它似乎可以很好地處理較簡單的查詢。奇怪的PDO MySQL問題

下面是該查詢:

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客戶端。

+1

腳本內發生了什麼?是否生成或輸出了任何錯誤? (如果您提供更多信息,您可以使人們更容易地獲得幫助。) – 2010-01-14 20:34:30

+0

-1:「不起作用」永遠不是一個足夠的問題描述。 – hobodave 2010-01-14 21:40:16

+0

只是問了一個明顯的問題,在你的問題中,newPDO是一個錯字嗎?它應該是新的PDO()來實例化類 – iangraham 2010-01-15 05:34:53

回答

0

我通過使用LEFT JOIN而不是INNER JOIN來解決問題。奇怪,但它工作。

2

嘗試啓用錯誤,如果尚未:

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

在舊版本的PDO使用的你有你把它聲明出於某種原因爲「初始化」你PDO變量。嘗試在此代碼之前添加一行,一般初始化$dbhDevices並查看是否有這個竅門。我有我的疑惑,因爲你說它適用於較簡單的查詢,但值得一試。