Fetching Data from database

I have a website which needs otp for registering, what code should be written to fetch data from database and fill in the otp textbox?

good starting point is, as often, documentation