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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gmac63@charter.net

    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($d b,"select dir,app,protoco l,sport,dport,m ark
    from iptmarkView");

    $row = sqlite_fetch_ar ray($result);

    while ($row = sqlite_fetch_ar ray($result)){
    print "Direction: $row[dir]\n<br>";
    print "Applicatio n: $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_ar ray($result)){
    print "Protocol: $row[protocol]\n<br>";
    print "Source Port: $row[sport]\n<br>";
    print "Destinatio n 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($d b,"select dir,app,protoco l,sport,dport,m ark
    from iptmarkView");

    $row = sqlite_fetch_ar ray($result);

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

    $db = sqlite_open('tc .db');
    $result = sqlite_query($d b,"select dir,app,protoco l,sport,dport,m ark
    from iptmarkView");

    $row = sqlite_fetch_ar ray($result);

    while ($row = sqlite_fetch_ar ray($result)){
    print "Protocol: $row[protocol]\n<br>";
    print "Source Port: $row[sport]\n<br>";
    print "Destinatio n 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

  • Andy Jeffries

    #2
    Re: database, *_fetch_array() ,while() issue.

    On Tue, 09 May 2006 13:16:28 -0700, gmac63 wrote:[color=blue]
    > ... it doesn't want to print the second while() loop unless I do the query
    > again:[/color]

    The reason is that sqlite_fetch_ar ray (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

    Comment

    • reandeau

      #3
      Re: database, *_fetch_array() ,while() issue.

      You have reached the last row of your result set using
      sqlite_fetch_ar ray($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

      Comment

      • gmac63@charter.net

        #4
        Re: database, *_fetch_array() ,while() issue.

        Andy, Reandau,

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

        Thanks.

        -Wes

        Comment

        Working...