gpt4 book ai didi

javascript - 我可以在前端 Javascript 中运行 SQL 查询吗?

转载 作者:可可西里 更新时间:2023-10-31 23:10:42 24 4
gpt4 key购买 nike

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

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

我正在使用以下 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
});

最佳答案

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

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

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

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

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

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

查看用于管理 Ajax 调用的 jQuery: http://api.jquery.com/jquery.ajax/

编辑您可以使用服务器端 javascript 访问您的数据库,here你可以找到一篇关于 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.

关于javascript - 我可以在前端 Javascript 中运行 SQL 查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37453671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com