Javascript 在 JS 中运行 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37453671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:21:17  来源:igfitidea点击:

Run a SQL query in JS

javascriptphpjquerysql

提问by John

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.

我想在 .JS 文件中运行 SQL 查询。我尝试向其中添加一些 PHP 代码,但它当然不起作用。

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?

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

I am using the following JavaScript code:

我正在使用以下 JavaScript 代码:

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

回答by Ema.jar

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.

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

You can find some information about client server here:

您可以在此处找到有关客户端服务器的一些信息:

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

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

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

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

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

你可以在这里找到很多关于 ajax 的有用信息:

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

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/

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

EDITYou can use server side javascript to access your database, hereyou can find a good article about MySql Cluster. In a nutshell:

编辑您可以使用服务器端 javascript 来访问您的数据库,在这里您可以找到一篇关于 MySql Cluster 的好文章。简而言之:

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.

Node.js 的 MySQL 集群 JavaScript 驱动程序就像它听起来的那样——它是一个连接器,可以直接从你的 JavaScript 代码中调用来读取和写入你的数据。由于它直接访问数据节点,因此通过 MySQL 服务器没有额外的延迟,并且需要从 JavaScript 代码//对象转换为 SQL 操作。如果出于某种原因,您希望它通过 MySQL 服务器(例如,如果您将表存储在 InnoDB 中),则可以对其进行配置。

回答by Elzo Valugi

You cannot connect to Mysql directly from client side JS, but only from server side JS, such as node. It can just like PHP connect using PDO or mysqli extensions, run your query and then pass the information to client side JS.

您不能直接从客户端 JS 连接到 Mysql,而只能从服务器端 JS,例如node. 它可以像使用 PDO 或 mysqli 扩展的 PHP 连接一样,运行您的查询,然后将信息传递给客户端 JS。

回答by Bhavin

Execute your query out side the JS and pass the prices in the javascript .

在 JS 之外执行您的查询并在 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,

现在在 Javascript 中添加 Like,

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

If you want add more than one prices then you can manage it through array also make array of price and pass it in the js.

如果您想添加多个价格,那么您可以通过数组进行管理,也可以制作价格数组并将其传递到 js 中。

回答by Abhishek Gurjar

You can't directly connect to mysql with javascript but you can echo out your php variable in javascript like this..

您无法使用 javascript 直接连接到 mysql,但您可以像这样在 javascript 中回显您的 php 变量。

 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 the whole javascript inside php script.

您可以在 php 脚本中覆盖整个 javascript。

this will replace like this on executing.

这将在执行时像这样替换。

{y: '2016 Q1', item1: 12054, item2: 65242},