Fetch next row from database query
Why does this simple function not work in my code?
Code:
class queryFactoryResult {
...
function FetchObject() {
return @mysqli_fetch_object($this->resource);
}
...
Usage:
Code:
$the_query = $db->Execute($query);
...
if ($the_query->RecordCount() > 0) {
$atom = $the_query->FetchObject();
$orders_id[$loop] = $atom->order_id;
...
$loop++;
...
Re: Fetch next row from database query
I have no idea where that code came from, nor the ideas behind it.
The way the Zen Cart database query infrastructure works is this:
Code:
$sql = "select * from " . TABLE_TABLENAME . " WHERE field='value'";
$result = $db->Execute($sql);
while (!$result->EOF) {
$my_variable = $result->fields['fieldname'];
// and/or do something with that $my_variable value
$result->MoveNext(); // to tell it to advance to the next record
} // this loops back to the "while" loop, repeatedly, until the EOF is triggered when all the results have been returned.
In v155 the code is even simpler, but you specified v154 so I gave you the old syntax.
Re: Fetch next row from database query
The code is from /includes/classes/db/sql/mysql/queryFactory - a deprecated part?
Re: Fetch next row from database query
I.e. I added the FetchObject-code with the same syntax (?) as RecordCount().
Re: Fetch next row from database query
I explained the correct usage and syntax above.
There's no need to change the query_factory.php code.
Re: Fetch next row from database query
Fair enough. Is there a way to reset a query to start, without redoing it - ex. $result->MoveTop()
Re: Fetch next row from database query
Not reliably before v155.
Why exactly are you wanting to rewind to start?
Re: Fetch next row from database query
A little bit messed up - will start thinking straight after aligning with ZenCart db-procedures.
Re: Fetch next row from database query
The syntax is definitely much easier after upgrading to v155:
Code:
$sql = "foo";
$result = $db->Execute($sql);
foreach ($result as $row) {
echo $row['fieldname'];
}
And also supports proper Iterator and Countable actions such as reset(), count(), as well as Move(0) and more.
... but you need at least v1.5.5 to have these available.
Re: Fetch next row from database query