If this is your first visit, check our documentation. To post, register or login.
Forums / General Questions / General Questions / Fetch next row from database query

Fetch next row from database query

Started by kalastaja on 7 Aug 2016, 8:06 PM · Views: 1,715

Results 1 to 12 of 12
7 Aug 2016, 8:06 PM
#1
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #1 by kalastaja

Why does this simple function not work in my code?

class queryFactoryResult {
...
  function FetchObject() {
    return @mysqli_fetch_object($this->resource);
  }
...

Usage:

    $the_query = $db->Execute($query);
... 
    if ($the_query->RecordCount() > 0) {
      $atom = $the_query->FetchObject();
      $orders_id[$loop] = $atom->order_id;
...
      $loop++;
...
7 Aug 2016, 10:00 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #2 by drbyte

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:

$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.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Aug 2016, 4:02 AM
#3
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #3 by kalastaja

The code is from /includes/classes/db/sql/mysql/queryFactory - a deprecated part?

8 Aug 2016, 4:20 AM
#4
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #4 by kalastaja

I.e. I added the FetchObject-code with the same syntax (?) as RecordCount().

8 Aug 2016, 5:09 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #5 by drbyte

I explained the correct usage and syntax above.
There's no need to change the query_factory.php code.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Aug 2016, 6:30 PM
#6
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #6 by kalastaja

Fair enough. Is there a way to reset a query to start, without redoing it - ex. $result->MoveTop()

8 Aug 2016, 7:37 PM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #7 by drbyte

Not reliably before v155.

Why exactly are you wanting to rewind to start?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Aug 2016, 7:40 PM
#8
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #8 by kalastaja

A little bit messed up - will start thinking straight after aligning with ZenCart db-procedures.

8 Aug 2016, 7:46 PM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #9 by drbyte

The syntax is definitely much easier after upgrading to v155:

$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.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Aug 2016, 7:48 PM
#10
kalastaja avatar

kalastaja

Zen Follower

Join Date:
Mar 2010
Location:
Finland
Posts:
465
Plugin Contributions:
0

Post #10 by kalastaja

Ok, Thanks.

9 Aug 2016, 6:42 PM
#11
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,147
Plugin Contributions:
56

Post #11 by lat9

DrByte:

Not reliably before v155.

Why exactly are you wanting to rewind to start?
Really? I've been using the following construct for a couple of years now to "rewind" a previously-processed query back to the beginning:

$result = $db->Execute (<some sql query>);
while (!$result->EOF) {
    ...
    $result->MoveNext ();
}
...
// -----
// "Rewind" the query to re-process ...
//
[B]$result->Move (0);
$result->MoveNext ();[/B]
while (!$result->EOF) {
    ...
    $result->MoveNext ();
}
11 Aug 2016, 11:09 AM
#12
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Post #12 by mc12345678

As suggested here, "reliability" (backwards compatibility to before ZC 1.5.5) can be obtained by testing for the method rewind as suggested by DrByte:

$result->Move(0);
if (!method_exists($result, 'rewind')) {
  $result->MoveNext();
}

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...