Node JS - Node js MySQL Select From - Node - Node JS tutorial - webnode




Selecting From a Table:

  • To select data from a table in MySQL, use the "SELECT" statement.

Example

Select all records from the "
Wikitechy_customers" table, and display the return object:
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM wikitechy_customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});
  • Save the code above in a file called "wikitechy_db_select.js" and run the Node JS Tutorial File:
Run "wikitechy_db_select.js"
C:\Users\Your Name>node wikitechy_db_select.js

Output:

[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id: 2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy', address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address: 'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty', address: 'Green Grass 1'},
  { id: 8, name: 'Richard', address: 'Sky st 331'},
  { id: 9, name: 'Susan', address: 'One way 98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 12, name: 'William', address: 'Central st 954'},
  { id: 13, name: 'Chuck', address: 'Main Road 989'},
  { id: 14, name: 'Viola', address: 'Sideway 1633'}
]

The Result Object:

  • To return e.g. the address of the third record, just refer to the third array object's address property:
Related nodejs article tags - node js - node js tutorial - node js examples

Example

Return the address of the third record:
console.log(result[2].address);

Output:

Apple st 652

The Fields Object

  • The third parameter of the callback function is an array containing information about each field in the result.

Example

Select all records from the "wikitechy_customers" table, and display the fields object:
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT name, address FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(fields);
  });
});

  • Save the code above in a file called "wikitechy_db_select_fields.js" and run the Node JS Tutorial File:
Run "wikitechy_db_select_fields.js"
C:\Users\Your Name>node wikitechy_db_select_fields.js

Output:

[
  {
    catalog: 'def',
    db: 'mydb',
    table: 'wikitechy_customers',
    orgTable: 'wikitechy_customers',
    name: 'name',
    orgName: 'address',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  },
  }
    catalog: 'def',
    db: 'mydb',
    table: 'wikitechy_customers',
    orgTable: 'wikitechy_customers',
    name: 'address',
    orgName: 'address',
    charsetNr: 33,
    length: 765,
    type: 253,
    flags: 0,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true
  {
  • As you can see from the result of the example above, the fields object is an array containing information about each field as an object.
  • To return e.g. the name of the second field, just refer to the second array item's name property:

Example

Return the name of the second field:
console.log(fields[1].name);

Output:

address

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js MySQL Select From