I am building a url shortner application in php and mysql and
i am using the follwing logic to create short urls, pls tell me how is it

First i fetch the last id from the database using

select max(id) from urls

then suppose if it returns 100 then i convert it into base62 number using this function
suppose it returns x31 then short url will be http://somedomain.com/x31

My question is that is this logic works even when large number of people accessing site at the same time or there will be clash in max(id)

4 Years
Discussion Span
Last Post by Yorkiebar14

Without knowing more about the queries users will be placing against the database it is hard to be sure but making sure all inputting transactions are wrapped in transactions should stop most conflicts.
Any solution that involves locking the required columns or tables during queries should mean max(id) always returns the correct url.


Your logic is mostly right. However, whenever I make projects like these, I tend to add an additional feature/column on to my tables which hold a randomly generated string (or, 'ticket') which I then use to reference from $_GET to MySQL(i) tables.
If you're going to use tickets always ensure that you're generating an unused ticket before you insert it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.