Is it possible to access a MySQL database straight from Javascript

Is there a way to access a MySql database using entirely client side Javascript or do you need to go through a server side language such as PHP or C#?

Thanks

Answers:

Answer

Javascript, if run in the browser, has no way of accessing a MySQL Database. For one, this is a technical limitation, because Javascript has no way of communicating arbitrary protocols (no, WebSockets are not the solution). Note that Node.js, being server side and all, is a "different kind of javascript".

Then there is the security issue. If your javascript could access the database directly, I could easily access the database myself. I would be able to read and manipulate the same data your javascript can. Security-wise calling this a nightmare would be an euphemism.

You'll have to - and WANT TO - route database access through a server side application. If that app is written in PHP, C# or Assembly doesn't really matter much. With Node.js you can even use Javascript on the server side. Use what you're comfortable with.

Answer

You need to go through the server. Giving the client-side code direct access to the database would be a massive security hole, even if you could do it.

Note that you can use JavaScript on the server, you don't have to use PHP or C#. There are several server-side JavaScript engines you can use, NodeJS (which runs Google's V8 engine) and Rhino (which runs on the Java stack) being two of them. And of course, Microsoft has supported JScript on the server for at least 15 years.

Answer

Yes, you can use a Javascript library like Node.js to perform MySQL queries in Javascript - it's just a really bad idea (TM) to do this client side, as you'd need to send users the authentication details and provide the ability for clients to connect to your MySQL server, meaning it'd have to be internet accessible to any IP! Not good. However if you really wanted just because you prefer Javascript but you only intend to do it on the server, try: (What MySQL drivers are available for node.js?).

What you should do is implement a small server-side script perhaps in PHP or Perl or Python or C#, which outputs just the data you want from the query and use AJAX to process the response.

Answer

You need to use a server side language. Otherwise that would be a huge security hole. Anyone could access this database and send arbitrary SQL queries to it.

Answer

For databases like MySql, you definitely should use a server side language. If you want javascript to be your only language, I suggest you check out nodejs or couchDB

Answer

In theory, it should be possible using the newer features like web sockets, etc. But you shouldn't do it, considering you'd have to make your credentials public. Create a server side wrapper/negotiator script in whatever available language you'd like to use and call that one. You can as well use it to validate inputs (e.g. filter too absurd highscores or impossible values; depending on what you're doing).

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.