Jump to content

Datatypes for different fields in a database


erikla
Go to solution Solved by requinix,

Recommended Posts

I am quite new to PHP and MySQL. I am trying to figure out what datatypes to use for different fields in a Database. For ID (Identification) I use INT, for  name I use VARCHAR and for people to write text messages, I use LONGTEXT. For date and time I use DATETIME.

 

What I am unsure about, however, is what type to use for an email adress and for an IP address? I have been watching videos at Youtube about the Datatype for IP addresses, but they don't seem always to agree. I hope someone can put me in the right direction on those two.

 

Regards,

 

Erik

Link to comment
Share on other sites

An email is a string so it gets a string type.

 

An IP address... yeah. What are you going to do with it? As a string it gets stored as something you can easily read but it isn't suited very well for sophisticated searches; as a number ("127.0.0.1" is 2130706433) is great for exact or range searches but it needs formatting in code to get it into the normal A.B.C.D form useful for humans.

Link to comment
Share on other sites

Hi requinix

 

Good question. A number of years ago I created a guestbook for my website, with a lot of help from a tutorial. This tutorial did have the IP address for the computer as a field in the database table. I think they added the IP address field in order to be able to lock out certain computers if they applied spam or so, but I am not sure. Now I just need to "repair" the code to make it work on my new Webhotel - since then some of the code has been deprecated and outdated. I still want the table to contain the IP address from contributors. From what you say, I guess a string type will be appropriate. I think I will still be able to compare string against string, right? So VARCHAR for email addresses as well as IP addresses?

 

Erik

Edited by erikla
Link to comment
Share on other sites

  • Solution

Yup. You can still do exact searches for string IP addresses, of course, but you lose out on the ability to do a range search (such as "everybody using the IP addresses 127.0.0.1 - 127.0.255.255"). But if you don't need to do that then there's no problem.

 

[edit] Worth mentioning is that you can convert string number at any point in the future so what you choose now doesn't have to be set in stone.

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.