Hi I have the following code which selects some results from a table that I have. I haven't included the SQL/Table structure as I know the SQL statement operates as expected.
My problem is when wanting to "recycle" a prepared statement i.e. I want to just fetch
the first result and then fetchAll
of the results. When I fetchAll
, the result set only contains one result (the last result), but there should be two results.
$stmt = $conn->prepare(*SQL STATEMENT*);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS| PDO::FETCH_PROPS_LATE,'MyClass');
$oneResult = $stmt->fetch();
echo $oneResult->get_Name();
$allResults = $stmt->fetchAll;
foreach($allResults as $row){
echo $row->getField_Name();
}
Is it possible to use a fetchAll
and fetch
in succession without preparing a new statement?
Just use fetchall and then use the resulting array to get your first and then to iterate:
$stmt = $conn->prepare(*SQL STATEMENT*);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS| PDO::FETCH_PROPS_LATE,'MyClass');
$allResults = $stmt->fetchAll;
$oneResult = each($allResults);
echo $oneResult->get_Name();
foreach($allResults as $row){
echo $row->getField_Name();
}