2014-02-05 35 views
0

我正在改變我的方式,並將我的網站從使用mysql_query轉換爲PDO,我很難過。使用PDO從特定列和行中返回數據

之前,我會質疑我的數據庫和檢索特定的行這樣的特定列:

$getPhotos_query = "select * from resultsPhotos where fk_surveyID = 1 and fk_itemID = 123 order by resultPhotoID"; 
$getPhotos_result = mssql_query($getPhotos_query); 

$thisPhoto1 = mssql_result($getPhotos_result, 0, 'resultPhotoFile'); 
$thisPhoto2 = mssql_result($getPhotos_result, 1, 'resultPhotoFile'); 
$thisPhoto3 = mssql_result($getPhotos_result, 2, 'resultPhotoFile'); 

現在我看到的錯誤在我的方式,並轉換爲PDO這樣

$getPhotos_query = $conn->prepare('select * from resultsPhotos where fk_surveyID = :surveyID and fk_itemID = :itemID order by resultPhotoID'); 
$getPhotos_query->execute(array('surveyID' => 1,'itemID' => 123)); 
$getPhotos_result = $getPhotos_query->fetchAll(PDO::FETCH_OBJ); 

但現在我無法弄清楚如何從三個返回的行中檢索resultPhotoFile列。

回答

2

$getPhotos_result[0]->resultPhotoFile

+0

'$ getPhotos_result [1] - > resultPhotoFile'很棒 –

1
$thisPhoto1 = $getPhotos_result[0]->resultPhotoFile; 
$thisPhoto2 = $getPhotos_result[1]->resultPhotoFile; 
$thisPhoto3 = $getPhotos_result[2]->resultPhotoFile; 

但是,如果你喜歡用數組而不是對象時,你可以使用

$getPhotos_query->fetchAll(); 

,而不是默認爲PDO :: FETCH_BOTH

您可以檢查documentation找到最適合您的fetch_style。