470,849 Members | 1,166 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

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

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
3 1320
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
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
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.

Similar topics

3 posts views Thread by Mudge | last post: by
2 posts views Thread by M.A. Oude Kotte | last post: by
9 posts views Thread by Jerim79 | last post: by
4 posts views Thread by BD | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.