Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 599
JMK Avatar asked Mar 19 '12 09:03

JMK


People also ask

Can we use JavaScript to connect to MySQL database?

JavaScript is a client-side language that runs in the browser and MySQL is a server-side technology that runs on the server. You need to use the server-side language Node. js to connect to the MySQL Database.

Can JavaScript access database directly?

JavaScript can't directly access the database. You'll need to have some server-side component that takes requests (probably via HTTP), parses them and returns the requested data.

Can I use MySQL without PHP?

No you can't interact with MySQL through JavaScript or JQuery. You could use JavaScript and JQuery through your PHP pages if that would be a functionality you'd be interested in. You could also use another language compatible with MySQL. Otherwise interacting with MySQL straight through JavaScript/JQuery won't work.

How do I access an existing MySQL database?

To access a specific database, type the following command at the mysql> prompt, replacing dbname with the name of the database that you want to access: Copy use dbname; Make sure you do not forget the semicolon at the end of the statement. After you access a database, you can run SQL queries, list tables, and so on.


1 Answers

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.

like image 83
rodneyrehm Avatar answered Oct 05 '22 03:10

rodneyrehm