Professional Web Applications Themes

how to go to the previous and next record - MySQL

Hai, Is there any way you can go to the previous (and next) record in a table? For example here is a list of people: http://volkstuin.deds.nl/test/index.php To get the record with the previous Achternaam, it works fine with the code using MAX: $achter = $_GET["achter"]; $queryback = mysql_query("SELECT MAX(achternaam) FROM testje WHERE (achternaam < '$achter') ORDER BY achternaam"); while(list($achternaamback) = mysql_fetch_array($queryback)){ echo"<br><a href='persoon.php?achter=$achternaamback'>vorige</a> achternaam is: <b>$achternaamback</b>.<br><br>"; } However, when there are more than one Achternaam, it goes to the wrong record. I have been trying and searching and have a solution which should work, according to http://jan.kneschke.de/projects/mysql/groupwise-max and I thought ...

  1. #1

    Default how to go to the previous and next record

    Hai,

    Is there any way you can go to the previous (and next) record in a
    table?
    For example here is a list of people:
    http://volkstuin.deds.nl/test/index.php
    To get the record with the previous Achternaam, it works fine with the
    code using MAX:

    $achter = $_GET["achter"];
    $queryback = mysql_query("SELECT MAX(achternaam) FROM testje
    WHERE (achternaam < '$achter') ORDER BY achternaam");
    while(list($achternaamback) = mysql_fetch_array($queryback)){
    echo"<br><a href='persoon.php?achter=$achternaamback'>vorige</a>
    achternaam is: <b>$achternaamback</b>.<br><br>";
    }

    However, when there are more than one Achternaam, it goes to the wrong
    record. I have been trying and searching and have a solution which
    should work, according to
    http://jan.kneschke.de/projects/mysql/groupwise-max
    and I thought to use the next code

    $achter = $_GET["achter"];
    $voor = $_GET["voor"];
    $queryback = mysql_query("SELECT MAX(achternaam), voornaam FROM
    testje
    WHERE (achternaam < '$achter') ORDER BY achternaam, voornaam");
    while(list($achternaamback, voornaamback) =
    mysql_fetch_array($queryback)){
    echo"<br><a
    href='persoon.php?achter=$achternaamback&voor=$voo ornaamback'>vorige</a>

    achternaam is: <b>$achternaamback</b> en voornaam is:
    <b>$voornaamback</b>.<br><br>";
    }

    But mysql says: mysql_fetch_array(): supplied argument is not a valid
    MySQL result resource
    it does not want to select the field "voornaam" in combination with
    MAX.

    Does anyone know what I do wrong and is there a solution for my
    problem?


    greeting
    keitje

    keitje Guest

  2. #2

    Default Re: how to go to the previous and next record

    keitje schrieb: 

    Next - in which contex? Next Date, next Customer ...? If each of the
    records has its on primary key, it would be easy to retrieve exactly
    this record.

    HTH, Michael
    Michael Guest

  3. #3

    Default Re: how to go to the previous and next record

    keitje schrieb: 

    I can't spot any error in your SQL. I don't think there's anything wrong
    with it, but you can try the query without the MAX around vornaam.
    Possibly you got the column name wrong, or the database connection
    failed for some reason.
    I suggest doing something like this:

    if (! $queryback) {
    # An error occurred
    print mysql_error ();
    }

    BTW I'd seriously recommend using mysql_fetch_assoc. The overhead is
    minimal, and you gain a lot of flexibility in your queries. (If you
    don't like to write things like "$row ['MAX(achternaam)']", say
    "SELECT MAX(achternaam) AS max_achternaam)" in your query.)

    Regards,
    Jo
    Joachim Guest

Similar Threads

  1. Record Set & Next | Previous Links
    By drdagwood in forum Coldfusion - Getting Started
    Replies: 0
    Last Post: October 14th, 12:08 AM
  2. Field based on previous record
    By Mark in forum FileMaker
    Replies: 7
    Last Post: February 1st, 02:18 AM
  3. Next and Previous record
    By Rick in forum PHP Development
    Replies: 5
    Last Post: November 9th, 04:45 PM
  4. Copy value from previous record?
    By Steve in forum FileMaker
    Replies: 1
    Last Post: August 6th, 10:06 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139