By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

database, *_fetch_array(),while() issue.

P: n/a
To be brief, I have never encountered this where I make a database
query:

<?php

$db = sqlite_open('tc.db');
$result = sqlite_query($db,"select dir,app,protocol,sport,dport,mark
from iptmarkView");

$row = sqlite_fetch_array($result);

while ($row = sqlite_fetch_array($result)){
print "Direction: $row[dir]\n<br>";
print "Application: $row[app]\n<br>";
print "\n<br>";
};

// No problem so far ... but if I want to print the rest (I have a
reason)...

while ($row = sqlite_fetch_array($result)){
print "Protocol: $row[protocol]\n<br>";
print "Source Port: $row[sport]\n<br>";
print "Destination Port: $row[dport]\n<br>";
print "Mark: $row[mark]\n<br>";
print "\n<br>";
};

?>

.... it doesn't want to print the second while() loop unless I do the
query again:

<?php

$db = sqlite_open('tc.db');
$result = sqlite_query($db,"select dir,app,protocol,sport,dport,mark
from iptmarkView");

$row = sqlite_fetch_array($result);

while ($row = sqlite_fetch_array($result)){
print "Direction: $row[dir]\n<br>";
print "Application: $row[app]\n<br>";
print "\n<br>";
};

$db = sqlite_open('tc.db');
$result = sqlite_query($db,"select dir,app,protocol,sport,dport,mark
from iptmarkView");

$row = sqlite_fetch_array($result);

while ($row = sqlite_fetch_array($result)){
print "Protocol: $row[protocol]\n<br>";
print "Source Port: $row[sport]\n<br>";
print "Destination Port: $row[dport]\n<br>";
print "Mark: $row[mark]\n<br>";
print "\n<br>";
};

?>

Why? It has to be quite simple, just never run into this before.

thx

-Wes Yates

May 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 09 May 2006 13:16:28 -0700, gmac63 wrote:
... it doesn't want to print the second while() loop unless I do the query
again:


The reason is that sqlite_fetch_array (as with all *_fetch_array
functions) has to maintain an internal record pointer so it know what the
next record to retrieve is. If it started from the beginning after
hitting the end of the result set, your first while loop would never end.

So after retrieving the last result in the first while loop, the record
pointer is set to say "no more records", so the first iteration of your
second while loop will find "no more records".

What I believe you want to do is issue an sqlite_rewind($result).

However, I've never used sqlite before, but that would definitely be
correct for MySQL.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

May 10 '06 #2

P: n/a
You have reached the last row of your result set using
sqlite_fetch_array($result). What you need to do is go back to the top
or your result set, this can be accomplished by using
sqlite_rewind($result). See
http://us3.php.net/manual/en/function.sqlite-rewind.php for more info.

Jon Tjemsland

May 10 '06 #3

P: n/a
Andy, Reandau,

Thanks, I (thought) I tried that but maybe not the right way. This
works perfectly.

Thanks.

-Wes

May 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.