Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: mysql auto_incremented id

by cchampion (Curate)
on Dec 17, 2003 at 22:08 UTC ( [id://315398]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-19 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found