What is the best datatype for storing URLs in a MySQL database?
The ideal would be something that doesn't take up a lot of space but can easily handle variable length URLs.
What is the best datatype for storing URLs in a MySQL database?
The ideal would be something that doesn't take up a lot of space but can easily handle variable length URLs.
If by "links" you mean links to web pages, I'm guessing you want to store URLs.
Since URLs are variable length strings the VARCHAR
data type would seem the obvious choice.
I recommend a VARCHAR field and the length is really personal opinion on what kind of URLs you are anticipating storing. You can get away with VARCHAR(255) without increasing the storage requirements for a VARCHAR field without penalty.
CREATE TABLE example(
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
url varchar(125)
);
$url = 'http://www.google.com';
$sql = 'INSERT INTO example SET url = ' . mysql_real_escape_string($url);
You can store URLs as simple strings, but be careful when comparing values since URLs may come in various forms while actually representing the same location (for example with/without the initial protocol identifier, or with/without a trailing slash).