How to connect to database for autocomplete when we need to choose previously selected values. Comma separated values.

How to connect to database for autocomplete when we need to choose previously selected values. Comma separated values.

I am facing issue to connect to database to get the autocomplete values. And i need to chose the previously selected values which will be comma separated.
Any help will be appreciated.

Thanks.