douyueqing1530 2018-09-11 18:10
浏览 47

加入表时PDO fetch_assoc [重复]

This question already has an answer here:

fetching an assoc-array with PDO seems to not show all resulting columns when joining tables.

<?php
$stmt = $GLOBALS['db']->prepare("
SELECT * 
  FROM printer
     , printermanufacturer
     , printermodel
     , colorprofile
     , papersize 
 WHERE User = :userID 
   AND printer.Manufacturer = printermanufacturer.ID 
   AND printer.Model = printermodel.ID 
   AND printer.Colorprofile = colorprofile.ID 
   AND printer.papersize = papersize.ID
");

$stmt->bindValue(':userID', $userID, PDO::PARAM_INT);
$stmt->execute();
print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
?>

This should result in an associative array with e.g. printermanufacturer.Name and printermodel.Name, but it doesn't. There is only one row called Name. Is there a solution without renaming each and every column like SELECT printermanufacturer.Name AS pmName ...

Thanks

edit: In the duplicate question there was also the (unanswered) question about: "is there a way to automatically have the t2 columns be identified as t2.col1, t2.col2, (etc)" Is this possible?

</div>
  • 写回答

1条回答 默认 最新

  • dounai6626 2018-09-11 18:15
    关注

    PDO::FETCH_NAMED should help

    PDO::FETCH_NAMED: returns an array with the same form as PDO::FETCH_ASSOC, except that if there are multiple columns with the same name, the value referred to by that key will be an array of all the values in the row that had that column name

    评论

报告相同问题?