faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 6 people (83%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

How can I store user information in a database?

Aug 5th, 1999 22:36
Nathan Wallace, M.Brands


Why not create one table with information about each user. Use the
username as a unique index (primary key). In a second table, start
adding records identified by the username (could be used as a foreign
key). Then, you can simply retrieve all records from the second table
with a single SQL statement.  Something like:

    create table users(username char(10),email text);
    create table logins(username char(10), time datetime);

(You probably want an index on the username column. It should really
speed things up.)

In the first table, you would insert one record for each user. Example
data could look like:

users:          username   | email
                -----------+------------------------------------
                jarjar     | shrike@il.fontys.nl
                kibo       | pesh@tosh.net
                benny      | bfore@sugarshack.com

logins:         username   | time
                -----------+------------------------------------
                jarjar     | 11:17 - 9 september 1998
                benny      | 0:43 - 29 july 1999
                benny      | 0:44 - 29 july 1999
                benny      | 12:50 - 1 august 1999

Get a list of logins for benny:

    select time from logins where username = 'benny'

This maintains a list of login times and dates. It's just an example
though. You probably want to do something more useful ;)