Deepjyoti Bora
Deepjyoti Bora

Reputation: 61

Why api is used when you can fetch data directly from database using sql query?

I am a newbie to the web application. I just understood what is an api and why it is used by reading some online blog. But I was wondering why to use an api to fetch data(or insert data) when you can directly fetch data from database using PHP and mysql.(I am sorry if this question sounds stupid.) An answer with example would be great. Thanks

Upvotes: 5

Views: 2059

Answers (1)

S.Gartmeier
S.Gartmeier

Reputation: 491

API are used to make communications more secure. With an API you can add encryption, different users and roles and a lot more. With MySQL you can not do that on the same level.

Future more, MySQL is a service, and if you work directly with it, it has to be open on a Port. Here you can not add not as well protection as you can on a Webserver.

An API can also add some logic. Maybe you want to control the input given by a request to it. Or maybe you want to have some additional calculations going on, before you make any INSERT or UPDATE to the database. This can help you to have your database clean.

If the System where the database and the API is located at, decides to change the database from lets say MySQL to PostgreSQL or anything else, every service connecting to it will have to change its code to make it work with the new environment.

So, an API can be more secure and has some standard everyone can rely on, even if the APIs background changes.

Upvotes: 4

Related Questions