简体   繁体   English

如何使用javascript查询db并创建数组

[英]How to use javascript to query db and create array

I have a database with the following table:我有一个包含下表的数据库:

Product_table

The product table has 3 columns;产品表有 3 列; product_name , product_info , and product_cost product_nameproduct_infoproduct_cost

I need to query the database in order to pull all product names out, and put them in an array.我需要查询数据库以提取所有产品名称,并将它们放入一个数组中。

I know the SQL is 'SELECT product_name FROM product_table' but I don't know how to implement that in javascript.我知道 SQL 是'SELECT product_name FROM product_table'但我不知道如何在 javascript 中实现它。

Any guidance would be greatly appreciated任何指导将不胜感激

Edit : See here if it helps: Is it possible to access an SQLite database from JavaScript?编辑:看看这里是否有帮助: 是否可以从 JavaScript 访问 SQLite 数据库?

Your question is very badly asked.你的问题问得很糟糕。

I suggest you to use node.js: https://nodejs.org/en/download/ It allows you to run javascript without browser.我建议你使用 node.js: https://nodejs.org/en/download/它允许你在没有浏览器的情况下运行 javascript。

And I assume the database is MySQL.我假设数据库是 MySQL。 Then u will need the mysql package: https://github.com/mysqljs/mysql Refer the github documentation on how to install and use it. Then u will need the mysql package: https://github.com/mysqljs/mysql Refer the github documentation on how to install and use it.

Basically, you will use the mysql pacakge to make connection to your database (you need to provide the host/user/password), create database connection, then executes your SELECT query.基本上,您将使用mysql连接到您的数据库(您需要提供主机/用户/密码),创建数据库连接,然后执行您的 SELECT 查询。 The mysql package's conn.query(sql, params) already returns list of results in an array for you. mysql包的conn.query(sql, params)已经为您返回了数组中的结果列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM