-2

How can I connect to mysql DB from JS function?

I found this question:

How to connect to SQL server database from javascript?

but it doesn't work on Chrome, because they're using ActiveXObject.

Thank you.

Community
  • 1
  • 1
Patrik18
  • 329
  • 5
  • 9
  • 18
  • 4
    In the question you linked, the accepted answer details the better ways of connecting to a database (server side code accessed by AJAX). It's a much better idea to do it this way. – Codesleuth May 10 '13 at 12:06
  • 2
    I would avoid it and prefer to do every DB connection on the server side (e.g. PHP) and get the data via AJAX. Why do you want to do it directly in JavaScript? – Mr. B. May 10 '13 at 12:08
  • of course I know about option connect through php script, but I hae cross-domain problem, so I just need to do simple select from db – Patrik18 May 10 '13 at 12:13
  • Maybe there is an option to fix your cross domain problem? – Kasyx May 10 '13 at 12:16
  • I posted about here: `http://stackoverflow.com/questions/16403729/cross-domain-wamp-node-js` – Patrik18 May 10 '13 at 12:19

6 Answers6

4

There is no good solution on web browser side which will allow you to work with all browsers. There are many disadvantages to work with database on browser site.

First of all, you show your database structure and it is very dangerous. Imagine, how easier is to make SQL-injection while you know tables and fields?

You have to establish connection in some way using password which will be shown to third party users. Or you have to set password-less connection, which is also dangerous.

When you establish connection with database, somebody can easly execute own query what is trivial, because you are showing your structure.

I strongly recommended you to not do it on browser side.

Kasyx
  • 3,170
  • 21
  • 32
  • eas, I understand, but I have cross-domain problem, I posted about: `http://stackoverflow.com/questions/16403729/cross-domain-wamp-node-js` so I just need to co some simple select – Patrik18 May 10 '13 at 12:18
2

but I have cross-domain problem, so I just need to do simple select from db

Direct browser-to-database communication is definitely not a proper solution.

If you want to get a list of values out of the db, just write a method in whatever server-side language you prefer (or need) to use, make the client-side JavaScript code call that method through its public URI and parse the response body into some data structure.

A more general solution to this kind of problems is XMLRPC (for the record, I've used it under Code Igniter / Flash ActionScript 3.0), though it's not all that simple to use.

If you need to get data from two different domains, then implement the above on both of them and make the JavaScript code call the two different URIs and combine the data (if needed).

Marcello Romani
  • 2,967
  • 31
  • 40
  • What do you exactly mean "call that method through its public URI" ? Can you show some example ? thx – Patrik18 May 10 '13 at 20:50
  • One of the duties of a "web framework" (CodeIgniter, Catalyst) is to map URIs to class methods. In simple words, that means deciding which code to execute when a user visits a certain URL, for which the web application is responsible. Example: when you visit www.simplemath.org/basicops/sum?a=1&b=2 the web application outputs 3 . In a simple but realistic scenario, you'd have a class named BasicOps which would have a sum() method. That method would be passed the two parameters, would sum them and send the output to the browser. – Marcello Romani May 11 '13 at 13:50
1

You must use AJAX, because JavaScript itself can't connect to server. You can call some PHP script with AJAX and in JavaScript handle response from it. See jQuery.ajax().

Pavel Štěrba
  • 2,822
  • 2
  • 28
  • 50
1

make Ajax call from your javascript to php which connects you to database

praveen
  • 420
  • 4
  • 18
1

Without some sort of plugin, or sending requests to a server side application that will access the database for you, you can't. So focus on those instead, specially server side apps.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
1

A comment under this question

How to make a database connection

suggests that's only possible with a particular mix of Microsoft technologies specifically designed to deploy desktop-like software using a web browser. The bits mentioned there are:

  • HTA - HTML Applications
  • JScript - a Microsoft flavor of JavaScript
  • ActiveX objects

A link is also provided: Introduction to HTML Applications (HTAs)

Community
  • 1
  • 1
Marcello Romani
  • 2,967
  • 31
  • 40