سؤال

I'm using PHP PDO with MS SQL Server. The following works just fine. The results are an array of locations, as expected.

function getAllCentersOld() {
global $fpdo, $pdo;
$query = $pdo->query("SELECT" 
    ." [LOCID]"
    .", [LOCNAME]"
    .", [LOCSHORT]"
    .", RIGHT('00'+ CAST([CENTER] as varchar(4)),4) as CENTER"
    .", [LAT]"
    .", [LONG]"
    .", [LOCATION_ID]"
    ." FROM locs2 with (nolock) WHERE LOCNAME <> 'Someplace'"
    ." ORDER BY LOCNAME asc");
$result = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
    $rows[] = $row;
}
return json_encode($rows);
}

But, when I try to use FluentPDO to build out the query, I can't seem to use the select method. I'm getting the error that follows.

function getAllCenters() {
global $fpdo, $pdo;
$query = $fpdo->from('locs2')
        ->select('[LOCID], [LOCNAME], [LOCSHORT]')
        ->where(array("LOCNAME <> ?" => "Someplace"))
        ->orderBy("LOCNAME asc")
        ;
$result = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
    $rows[] = $row;
}
return json_encode($rows);
}

Uncaught exception 'Exception' with message 'Objects returned by SelectQuery::getIterator() must be traversable or implement interface Iterator' in ....{remove}... FluentPDO/SelectQuery.php:116 ... SelectQuery::fetchAll() ...

If I comment out the select line above, it works. Otherwise I get the following error. Is anyone familiar with this library? I thought it would operate as on the Web site.

http://fluentpdo.com/documentation.html#select

هل كانت مفيدة؟

المحلول 2

I think I just found the answer. The select "*" needs to be cleared out first. The following seems to work.

->select(null)->select('[LOCID], [LOCNAME], [LOCSHORT]')

I also need to revise the PDO parameter binding in the where() method. It seems that this library is a bit different than expected (notice 'PDO::FETCH_ASSOC' is not needed). Full working code below:

function getAllCenters() {
global $fpdo, $pdo;
$query = $fpdo->from('z_cc_locs2')
        ->select(null)->select("[LOCID], [LOCNAME], [LOCSHORT], RIGHT('00'+ CAST([CENTER] as varchar(4)),4) as CENTER, [LAT], [LONG], [LOCATION_ID]")
        ->where("LOCNAME <> :name", array(':name' => 'Formosa'))
        ->orderBy("LOCNAME ASC");
$result = $query->fetchAll();
foreach ($result as $row) {
    $rows[] = $row;
}
return json_encode($rows);
}

نصائح أخرى

I don't know this library but I've look at the source at github. $query is not a PDOStatement object so fetchAll like you called it will not work.

$query is a SelectQuery object which also have a fetchAll method but with other arguments:

/** Fetch all row
 * @param string $index  specify index column
 * @param string $selectOnly  select columns which could be fetched
 * @return array of fetched rows
 */
public function fetchAll($index = '', $selectOnly = '') {
    ...

The easiest way would be to directly iterate the $query object because it implements IteratorAggregate. (not directly but through inheritance)

So following should work:

$rows = array();
foreach ($query as $row) {
    $rows[] = $row;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top