Running SQL query in JS

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

I want to run an SQL query: SELECT price FROM list WHERE q=1 . I want to show the price on the chart. Does anyone know how I can run this SQL query in a .JS file to get the price of an item from a database?

I am using the following JavaScript code:

  /* Morris.js Charts */ // Sales chart var area = new Morris.Area({ element: 'revenue-chart', resize: true, data: [ {y: '2016 Q1', item1: 5000, item2: 4460}, {y: '2016 Q2', item1: 8432, item2: 5713} ], xkey: 'y', ykeys: ['item1', 'item2'], labels: ['Item 1', 'Item 2'], lineColors: ['#a0d0e0', '#3c8dbc'], hideHover: 'auto' }); var line = new Morris.Line({ element: 'line-chart', resize: true, data: [ {y: '2015 Q4', item1: 0}, {y: '2016 Q1', item1: 5000}, {y: '2016 Q2', item1: 8432} ], xkey: 'y', ykeys: ['item1'], labels: ['Item 1'], lineColors: ['#efefef'], lineWidth: 2, hideHover: 'auto', gridTextColor: "#fff", gridStrokeWidth: 0.4, pointSize: 4, pointStrokeColors: ["#efefef"], gridLineColor: "#efefef", gridTextFamily: "Open Sans", gridTextSize: 10 }); 
+7
javascript jquery sql php
source share
4 answers

You cannot fulfill the request using javascript because javascript cannot directly connect to your database, but you can use AJAX. Using this technology, you can send a request to a page with PHP (or another language on the server side) where there will be code that can execute a request to your db and return the result of this request.

Here you can find information about the client server:

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

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

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

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

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

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

The JavaScript driver for the MySQL cluster for Node.js is what it sounds like - its connector, which you can call directly from your JavaScript code to read and write your data. When it accesses the data of nodes directly, there is no additional delay from passing through the MySQL Server and it is necessary to convert objects from JavaScript code to SQL operations. If for some reason you prefer it to go through MySQL Server (for example, if you store tables in InnoDB), then you can configure it.

+4
source share

You cannot connect to Mysql directly from JS on the client side, but only with JS on the server side, for example node . It can be the same as a PHP connection using PDO or mysqli extensions, launching your request, and then passing information on the JS side on the client side.

+3
source share

Run the request on the JS side and pass the prices in javascript.

Example:

 $res_price = $conn -> query("SELECT price FROM list WHERE q=1"); $row_price = mysqli_fetch_assoc($res_price); echo $row_price['price']; 

Now in Javascript add Like,

 <?php echo $row_price['price']; ?> 

If you want to add several prices, then you can control it through an array, also make an array of prices and pass it to js.

+3
source share

You cannot directly connect to mysql using javascript, but you can respond your php variable in javascript like this.

  var area = new Morris.Area({ element: 'revenue-chart', resize: true, data: [ {y: '2016 Q1', item1: <?php echo $price ?>, item2: <?php echo $price ?>}, {y: '2016 Q2', item1: 8432, item2: 5713} ] 

you can cover all javascript inside php script.

this will replace it at runtime.

 {y: '2016 Q1', item1: 12054, item2: 65242}, 
+2
source share

All Articles