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)

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.