5

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?

1
  • 2
    Let'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... Commented Apr 2, 2011 at 8:03

6 Answers 6

6

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/

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

Depends on what DB you want to use.

CouchDB is HTTP addressable, so can be hit from JS. http://couchdb.apache.org/

Comments

1

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

6 Comments

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.
@Nick - you're being silly :). checkout Node.JS. 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, 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?
Having more choices is always a good thing. There is an ever-growing community 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.
@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.
|
0

Yes it is.

I don't know more about it but javascript can connect with DB using ADODB.Connection.

Comments

0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.