在JS中运行SQL查询 [英] Run a SQL query in JS

查看:819
本文介绍了在JS中运行SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.JS文件中运行SQL查询.我试图向其中添加一些PHP代码,但是它当然没有用.

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.

我想运行SQL查询:SELECT price FROM list WHERE q=1.我想在图表中显示价格.有人知道如何在.JS文件中运行此SQL查询,以便从数据库中获取商品价格吗?

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

我正在使用以下JavaScript代码:

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
  });

推荐答案

您无法使用javascript执行查询,因为javascript无法直接与您的数据库连接,但是您可以使用AJAX.借助这项技术,您将能够向PHP(或其他服务器端语言)页面发送请求,该页面中驻留了可以对数据库执行查询并获取该查询结果的代码.

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/

您可以在此处找到许多有关ajax的有用信息:

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

https://developer.mozilla.org/zh-CN/docs/AJAX/Getting_Started

甚至看一下jQuery来管理Ajax调用: http://api.jquery.com/jquery.ajax/

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

编辑 您可以使用服务器端JavaScript来访问数据库,

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

听起来很像Node.js的MySQL Cluster JavaScript驱动程序 就像这样-它是可以直接从您的电话中调用的连接器 用于读取和写入数据的JavaScript代码.当它访问数据时 直接节点,通过MySQL没有额外的延迟 服务器,需要将JavaScript代码//对象转换为SQL 操作.如果出于某种原因,您希望它通过 MySQL服务器(例如,如果您要在InnoDB中存储表) 可以配置.

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.

这篇关于在JS中运行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆