Is it possible for javascript to access a database directly? I feel my question is rhetorical owing to the fact that this is a security issue. But is it possible anyway?
-
2Let's think about this question. Javascript is exposed to the client. If we connected to the database the connection info would be stored on ding ding ding the C L I E N T. Holy s*** batman... – The Muffin Man Apr 02 '11 at 08:03
6 Answers
It is possible!
with the new html5 feature, js can connect through WebSql.
a live example : http://html5demos.com/database
the syntax is similar to all the other sql wrappers :
var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE foo (id unique, text)');
});
it is currently supported by chrome, safari and opera
here's a tutorial : http://html5doctor.com/introducing-web-sql-databases/

- 41,171
- 10
- 96
- 108
Is it possible for javascript to access a database directly?
No. Setup a server side script which will talk the database and then call this script with AJAX.

- 1,023,142
- 271
- 3,287
- 2,928
Depends on what DB you want to use.
CouchDB is HTTP addressable, so can be hit from JS. http://couchdb.apache.org/

- 54
- 1
- 3
Not from the browser. Javascript can be used on the server to set up server side functionality, though.

- 24,530
- 33
- 104
- 151
-
jeez we might as well make it so you can use CSS on the server. Then we could modify a dos prompt to be the exclusive IDE. – The Muffin Man Apr 02 '11 at 08:11
-
@Nick - you're being silly :). checkout [Node.JS](http://nodejs.org/). I'm powering a very high traffic server off of server side JavaScript (Node.JS) that connects directly to a database (Redis) and it's working great. – Anurag Apr 02 '11 at 08:14
-
@Anurag, My question is why? Why use that when there is a plethora of awesome programming languages out there that have proved themselves on the server? Not only that, but if you have a question is there a large community to answer? – The Muffin Man Apr 02 '11 at 08:16
-
1Having more choices is always a good thing. There is an [ever-growing community](https://github.com/joyent/node/wiki/modules) of users and developers contributing towards this project. Why JavaScript in particular - because it's a great language, and moreover, if you are writing web applications that make good use of JS, then you could reuse the same code between server and client keeping things [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself). – Anurag Apr 02 '11 at 08:19
-
@Nick: The simple explanation is because of Javascript's limitations. When you are writing functional programs, you you need to make sure no part of your code makes synchronous calls, e.g. talking to the filesystem. Most scripting languages have aren't suitable for functional programming because they make calls that block the execution of the code. Javascript is unique because it doesn't make blocking calls, and when it has to talk to the filesystem it has to go through nodejs, which can synchronize these requests. – picardo Apr 02 '11 at 08:23
-
@Nick: Here is a better explanation: http://stackoverflow.com/questions/3436335/could-node-js-replace-ruby-on-rails-completely-in-the-future/3436693#3436693 – picardo Apr 02 '11 at 08:26
Yes it is.
I don't know more about it but javascript can connect with DB using ADODB.Connection.

- 1
http://www.daniweb.com/web-development/php/threads/197091/update-mysql-table-using-javascript You need to look into the jQuery.ajax function, this will send/receive information from a PHP document.
What you need to do is set up a PHP document that will handle the the form as though it were being posted to by http, or by setting the action on the tag.
You then need to make a function similar to this:

- 11