I have ran in to a pickle with using a while loop to iterate over a result set.
I execute a query, and pass into a class, storing it as a private variable.
Then I use two separate methods to do a while loop over the result set.
The first method works, the second doesn't.
Here's some code to show example.
<?php
class DataFlow {
private $resultSet;
function __construct($results) {
$this->resultSet = $results;
}
function method1() {
while ($rows = CDB::GetAssoc($this->resultSet)) {
// works here
echo $rows;
}
}
function method2() {
while ($rows = CDB::GetAssoc($this->resultSet)) {
// doesn't work here
echo $rows;
}
}
}
$sqlQuery = "....";
$results = CDB::ExecuteQuery($sql);
$dataFlow = new DataFlow($results);
echo $dataflow->method1(); // Works
echo $dataflow->method2(); // Doesn't work.
?>
Now if I edit this to include the query execution locally.. it works
<?php
class DataFlow {
private $resultSet;
function __construct($results) {
$this->resultSet = $results;
}
function method1() {
while ($rows = CDB::GetAssoc($this->resultSet)) {
// works here
echo $rows;
}
}
function method2() {
$sqlQuery = "....";
$results = CDB::ExecuteQuery($sql);
while ($rows = CDB::GetAssoc($results)) {
// now works here
echo $rows;
}
}
}
$sqlQuery = "....";
$results = CDB::ExecuteQuery($sql);
$dataFlow = new DataFlow($results);
echo $dataflow->method1(); // Works
echo $dataflow->method2(); // Works
?>
I can't seem to figure out why I can't use a result set twice in the same class. Any ideas?