mrpatg
mrpatg

Reputation: 10117

auto increment with a string of numbers and letters

Right now im using auto increment to identify resources in my website.

The problem is i dont want the users to know how many resources there are.

How could i instead use some kind of structured "random" combination of letters and numbers (say 6 digits) that i could use instead of (or in addition to) the auto incrementing primary key numbers.

Basically i want the users to see

website.com/page.php?4jnd32

instead of

website.com/page.php?13

I dont even know what this practice is called which hampers my ability to search for an answer

Upvotes: 0

Views: 14040

Answers (3)

Peter Bailey
Peter Bailey

Reputation: 105914

MySQL supports UUIDs. But you'll want to employ a trigger to get this working automatically.

CREATE TABLE `tester` (
  `tester_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` VARCHAR(64),
  `foo` VARCHAR(45),
  PRIMARY KEY (`tester_id`),
  INDEX `index_uuid`(`uuid`)
)
ENGINE = InnoDB;

delimiter |
CREATE TRIGGER tester_uuid BEFORE INSERT ON `tester`
FOR EACH ROW BEGIN
  SET NEW.uuid = UUID();
END;
|
delimiter;

INSERT INTO tester (foo) VALUES('bar'), ('baz');

SELECT * FROM tester;

Upvotes: 0

Daniel Brotherston
Daniel Brotherston

Reputation: 2051

You could use a GUID although this would be substantially longer than "4jnd32". A guid is a Globally Unique Identifier. It would identify pages uniquely, but it wouldn't give the user any idea how many pages there are or give an ordering to the page.

I'm not an expert on PHP, but a quick search seems to indicate that php has a function to generate GUIDs.

Upvotes: 0

erenon
erenon

Reputation: 19158

If you really want this, create a new field (e.g: fake_id) then use php's uniqid function.

Upvotes: 1

Related Questions