Run a SQL query in JS

后端 未结 4 491
挽巷
挽巷 2021-02-04 13:30

I want to run a SQL query in a .JS file. I tried to add some PHP codes to it but it didn\'t work of course.

I want to run the SQL query: SELECT price FROM list WH

4条回答
  •  执念已碎
    2021-02-04 13:56

    You can't execute a query using javascript because javascript can't connect directly with your database, but you can use AJAX. With this technology you'll be able to send a request to a PHP (or other server side language) page where resides the code that can execute a query to your db and get back the result of this query.

    You can find some information about client server here:

    https://spin.atomicobject.com/2015/04/06/web-app-client-side-server-side/

    http://www.codeconquest.com/website/client-side-vs-server-side/

    And you can find a lot of useful information about ajax here:

    https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

    Take a look even to jQuery for manage Ajax call: http://api.jquery.com/jquery.ajax/

    EDIT You can use server side javascript to access your database, here you can find a good article about MySql Cluster. In a nutshell:

    The MySQL Cluster JavaScript Driver for Node.js is just what it sounds like it is – it’s a connector that can be called directly from your JavaScript code to read and write your data. As it accesses the data nodes directly, there is no extra latency from passing through a MySQL Server and need to convert from JavaScript code//objects into SQL operations. If for some reason, you’d prefer it to pass through a MySQL Server (for example if you’re storing tables in InnoDB) then that can be configured.

提交回复
热议问题