If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
MySQL table से data select करने के लिए हमें सिर्फ MySQL Query change करनी पड़ती है , और जब भी हम Table से records fetch करते हैं तो हमें result में Objects का Array मिलता है , जिसमे Table Rows को Object से represent किया जाता है।
जब हमें table के सभी columns select करने होते हैं , तो SELECT *
// import mysql package.
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
host: "localhost",
user: "root",
password: null,
database : "node_mysql"
});
// make connection.
mysql.connect(function(err) {
if (err) throw err
//select query.
let query = `SELECT * FROM tbl_users`
mysql.query(query, (error, rows) => {
if (error) throw error
console.log(rows);
})
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js [ RowDataPacket {id: 1,first_name: 'Rahul',last_name: 'Kumar',email: 'info@learnhindituts.com',about_user: 'I am a student',create_date: 2022-01-09T03:22:59.000Z}, RowDataPacket {id: 2,first_name: 'Rohan',last_name: 'Sharma',email: 'rohan@gmail.com',about_user: 'About rohan',create_date: 2022-01-09T03:36:19.000Z}, RowDataPacket {id: 3,first_name: 'Shohan',last_name: 'verma',email: 'Shohan@gmail.com',about_user: 'About Shohan',create_date: 2022-01-09T03:36:19.000Z}, RowDataPacket {id: 4,first_name: 'Mohan',last_name: 'yadav',email: 'Mohan@gmail.com',about_user: 'About Mohan',create_date: 2022-01-09T03:36:19.000Z}, RowDataPacket {id: 5,first_name: 'Ravi',last_name: 'singh',email: 'Ravi@gmail.com',about_user: 'About Ravi',create_date: 2022-01-09T03:36:19.000Z} ]
जैसा आपने ऊपर पढ़ा कि Output में Objects का Array मिलता है , जिसमे Table Rows को Object से represent किया जाता है। अगर कोई data नहीं आया तो empty array [] मिलता है।
SELECT * करने पर हमें सभी columns मिलते हैं , अगर particular columns select करने होतो उन columns को MySQL query में define करना पड़ेगा।
See Example :
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
host: "localhost",
user: "root",
password: null,
database : "node_mysql"
});
mysql.connect(function(err) {
if (err) throw err
//select only first_name, last_name, email.
let query = `SELECT first_name, last_name, email FROM tbl_users`
mysql.query(query, (error, rows) => {
if (error) throw error
console.log(rows);
})
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js [ RowDataPacket {first_name: 'Rahul',last_name: 'Kumar',email: 'info@learnhindituts.com'}, RowDataPacket {first_name: 'Rohan',last_name: 'Sharma',email: 'rohan@gmail.com'}, RowDataPacket {first_name: 'Shohan',last_name: 'verma',email: 'Shohan@gmail.com'}, RowDataPacket {first_name: 'Mohan',last_name: 'yadav',email: 'Mohan@gmail.com'}, RowDataPacket {first_name: 'Ravi',last_name: 'singh',email: 'Ravi@gmail.com'} ]
तो कुछ इस तरह से MySQL Table से records fetch करते हैं , आप अपनी need के according query में where clause या other changes भी कर सकते हैं।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook