使用PDO从特定列AND行返回数据

时间:2014-02-05 18:54:27

标签: php mysql pdo

我正在改变我的方式并将我的网站从使用mysql_query转换为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 个答案:

答案 0 :(得分:2)

$getPhotos_result[0]->resultPhotoFile

答案 1 :(得分: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。