SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've selected all the records from a database for display, and am trying to form a loop so that it will display all records, but it's just displaying the same record 15 times (there are 15 rows in the database). I can't see where i've gone wrong! My code is below, if anyone can help I would be most grateful!

    Thanks,

    Jon.

    **The Code**
    $sql = "SELECT *
    FROM contacts";

    // Execute the query and put results in $result

    $result = pg_exec($sql);

    // Get the number of rows and put in $num

    $num = pg_numrows($result);

    // Start the table.

    echo "<P><center>";
    echo "<TABLE cellpadding=2 cellspacing=0 bgcolor=#050DA5>";

    // Loop for as long as $i <= $num

    while($i <= $num) {

    echo "<tr><td bgcolor=#80FF80>";
    echo pg_result($result,'$i','real_name');
    echo "<br>";
    echo pg_result($result,'$i','dob');
    echo "</td><td bgcolor=#80FF80>";
    echo pg_result($result,'$i','address1');
    echo "<br>";
    echo pg_result($result,'$i','address2');
    echo "<br>";
    echo pg_result($result,'$i','town');
    echo "<br>";
    echo pg_result($result,'$i','postcode');
    echo "</td><td bgcolor=#80FF80>";
    echo pg_result($result,'$i','email1');
    echo "<br>";
    echo pg_result($result,'$i','email2');
    echo "</td><td bgcolor=#80FF80>";
    echo pg_result($result,'$i','telhome');
    echo "<br>";
    echo pg_result($result,'$i','telmob');
    echo "<br>";
    echo pg_result($result,'$i','telpage');
    echo "</td></tr>";

    // increment to continue looping

    $i++;

    }

    // Finish the table and page.

    echo "</table></body></html>"

    ?>

    **End**

  2. #2
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this loop instead:

    <?

    while($data = mysql_fetch_array($result)) {

    echo $data['real_name'] . "<br>";
    echo $data['address1'] . "<br>";

    // etc.
    }

    ?>

    The while() loop will fetch a few row before each iteration of the loop and will exist when there are no more rows to get...

    Christian
    www.Christian.Reimer.com
    A personal website exploring the mental summersaults,
    odd occurrences and political powerplays in the land of me.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Except for the fact that he is using PostgreSQL for a db your code would work. Try

    <?
    $i = 0;
    while($data = pg_Fetch_array($result, $i)) {

    echo $data['real_name'] . "<br>";
    echo $data['address1'] . "<br>";

    $i++;
    }

    ?>


    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooop.. You are right of course! Guess I better go get myself another cup of coffee and start paying attention

    Christian
    www.Christian.Reimer.com
    A personal website exploring the mental summersaults,
    odd occurrences and political powerplays in the land of me.

  5. #5
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you for your help

    It's getting late now so I haven't got the time or the available brain cells to check it out tonight, but i'll be sure to try it tomorrow (and i'm sure it will work, so thank you in advance for your help!).

    Jon.

  6. #6
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you for your help with the loop.

    What with the festive season, essays, socialising, and problems with my web server I haven't been able to implement the code until now, and i'm experiencing a problem I don't know how to resolve!

    It prints all the records as I hoped but it gives me an error:

    Warning: Unable to jump to row 14 on PostgreSQL result index 2 in /path/to/my/file.inc on line 46

    Line 46 is the start of the loop:

    while($data = pg_Fetch_array($result, $i)) {

    The rest of the loop is as you gave me.

    Any ideas? There are only 14 records currently in the database, and there will only be 31 in total once i've added all the information, so if it would be easier to tell it only to search for 31 records then that would be fine.

    Once again thanks for all your help, and have a happy new year!

    Jon.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you need the $i in there you can just use
    while($data = pg_Fetch_array($result)) {

    print $data["fieldname"];
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •