Home » Node.js

Search record with two fields from MYSQL table by using Node.js

In this article, we are going to learn how to search record with two fields from MYSQL table using Node.js Server?
Submitted by Manu Jemini, on November 25, 2017


In this article, we are going to do is to prepare a server file of Node.js where we require the MySQL module to work with MySQL and after that, we prepare a connection by using mysql.createConnection() method of MySQL module.

There are certain things that we have to define inside our create connection methods like host, user, password, and database name.

As we are all set to go with our connection, all we need to create an SQL query to count data basically a select query and two fields name by which we can search the records from MySQL table and after that we can use query() method to execute query statement with a callback in which we can throw error if any and print the number of records available in MYSQL table.

Database details:

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database: demo
  • Table name: clients
  • Fields: id and name

Server File

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "",
  user: "root",
  password: "123",
  database: "demo"
con.connect(function(err) {
  if (err) throw err;
  var sql = "select * from clients where id='1' and name='Aman'";
  con.query(sql, function (err, result) {
    if (err) throw err;

Discussing steps that we have followed in our server file:

  • Require MySQL module.
  • Create Connection variable using mysql.createConnection() method.
  • Connect by using con.connect() method.
  • Creating a SELECT SQL query by using fields id and name.
  • Execute query by using .query() method.
  • Show result.


Node.js - search record with two fields

If you have any query do comment.

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.