简体   繁体   中英

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; product_name , product_info , and product_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.

Any guidance would be greatly appreciated

Edit : See here if it helps: Is it possible to access an SQLite database from JavaScript?

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.

And I assume the database is MySQL. 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. The mysql package's conn.query(sql, params) already returns list of results in an array for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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