AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Select From


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

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:

SELECT [column_name] FROM [table_name]

 

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”.

Example: Selecting Data From Table "Users":
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 * 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:

E:\Users\Your_Name>node demo_test_db_select.js
Note: "SELECT * FROM Users" will return all columns from Users table

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.

Example: select two columns:
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 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:

Example: : Run "demo_test_db_select2.js"
E:\Users\Your_Name>node demo_test_db_select2.js
Note: "SELECT id, name FROM Users" will return all columns id, name FROM Users

[
{ 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.

Example:

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:

Example: : Run "demo_test_multi_rows_db_insert.js"
E:\Users\Your_Name>node demo_test_db_select3.js

[
{
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'u142985959_ager'@'localhost' (using password: YES)