Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: mysql auto_incremented id

by jwlarson3rd (Acolyte)
on Dec 17, 2003 at 21:37 UTC ( [id://315390]=note: print w/replies, xml ) Need Help??


in reply to Re: mysql auto_incremented id
in thread mysql auto_incremented id

the reason that I use the id field is that the client wants a slide show with next,prev and exit buttons. the only way that I could figure out how to do this is passing the id for an image in an hidden field and incrementing or decrementing the id field to point to a previous or next image to be displayed. if there is a break in the id number sequence the code barfs. if I could pass id numbers that don't exist I could make the delete image option that the client wants.

Replies are listed 'Best First'.
Re: Re: Re: mysql auto_incremented id
by cchampion (Curate) on Dec 17, 2003 at 22:08 UTC

    How do you deal with first and last image then?

    I insist on my belief that a primary key should be doing only one task, i.e. identifying the record uniquely. If you need a display id, you have two choices:

    • Adding a field to your table for display purposes, but then you would have two further problems.
      • filling the gaps when you delete a record.
      • Always displaying the records in the same order. No chance of diplaying with different criteria.
    • Creating a display number when you select the records. And you can do it either in (at least) two ways:
      • with a MySQL variable.
        SELECT @count := @count+1 as sequence, id, name, filename from mytable
      • In your Perl script.
        my $query = "SELECT id, name, filename from mytable"; my $sth=$dbh->prepare($query); $sth->execute(); my @results; my $count =0; while (my $row = $sth->fetchrow_arrayref) { push @results, [ $count++ , @$row ] }
        Now your @results have a counter that you can use for displaying purposes.
        Modifying your query with a different WHERE or ORDER BY will change the display order.
Re: Re: Re: mysql auto_incremented id
by mpeppler (Vicar) on Dec 18, 2003 at 00:29 UTC
    The correct way to do this is with appropriate WHERE clauses.

    For example to get the next row in the sequence:

    select ... from the_table where id = (select min(id) from the_table where id > $last_id)
    An alternative, with MySQL, is to use the LIMIT keyword. IIRC this should work (note - I'm not a MySQL specialist, so check for correct syntax):
    select ... from the_table where id > $last_id order by id LIMIT 1
    As you are limiting the result set to a single row, and will get next row in the sequence even if there are gaps.

    Michael

Re: Re: Re: mysql auto_incremented id
by bradcathey (Prior) on Dec 18, 2003 at 14:02 UTC
    jwlarson3rd you asked a very similar, if not identical, question here. And I'll respond in kind by repeating the reply I gave then. Like the other monks have pointed out, an unbroken sequence of ids is unnecessary. And though there are potential holes in my method of displaying records with Next and Prev buttons (I'm relying on the DB returning records in order), I have not experienced problems. However, to shore it up a bit (to address the concerns of mpeppler) you may be able to do some housekeeping with a hidden input statement, cookie, or flat file).

    To your point of reclaiming lost ids, I have often wondered what happens if I'm using a datatype of TINYINT and exceed 256 because of deleting and adding records. So I have errored on the side of using datatypes that far exceed my needs.

    —Brad
    "A little yeast leavens the whole dough."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://315390]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-25 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found