Node.js MySQL Select From
Node.js Tutorial » Node.js MySQL Select From
How to Select data from MySQL database using node.js
We are going to use standard SELECT FROM SQL Query.
Syntax:
Node.js MySql Selecting Data From Table
To select data from a MySQL table, you have to use "SELECT" statement.
Select all records from a Table named “Users” under “my_db_name”.
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM Users", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file named "demo_test_db_select.js", and run the file:
After run the file will give you this result:
[
{ id: 1, name: 'Jack', address: 'Chearch 74'},
{ id: 2, name: 'Jony', address: 'Histreet 41'},
{ id: 3, name: 'Maria', address: 'More2 52'},
{ id: 4, name: 'Chris', address: 'Mountainst 12'},
{ id: 5, name: 'Micha', address: 'Valleycity 120'}
]
Related subjects:
node.js create database
nodejs create table
node.js Send Email
upload files
Tags: node js Select From array, mysql, table, select random from array,
how to return a value from a mysql select query in node js
how to fetch data from database in node js and display in html
node js mysql query parameters
select query with where clause in node js
nodejs sql query
nodejs mysql result to array
Select Specific Columns from Table
In the next example, we will select two columns: id and names from MySQL table Users.
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT id, name FROM Users", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file called "demo_test_db_select2.js", and run the file:
[
{ id: 1, name: 'Jack'},
{ id: 2, name: 'Jony'},
{ id: 3, name: 'Maria'},
{ id: 4, name: 'Chris'},
{ id: 5, name: 'Micha'}
]
Fields Object of MySQL SELECT
Fields Object is an array containing information about columns interrogating of table and each field contains all information about a column.
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT name, address FROM Users", function (err, result, fields) {
if (err) throw err;
console.log(fields);
});
});
Save the code above in a file called "demo_test_db_select3.js", and run the file:
[
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'name',
orgName: 'name',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
},
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'address',
orgName: 'address',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
}
]
node js Select From array, mysql, table, select random from array,
how to return a value from a mysql select query in node js
how to fetch data from database in node js and display in html
node js mysql query parameters
select query with where clause in node js
nodejs sql query
nodejs mysql result to array
Node.js MySQL Select From - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.