NodeJS Slip 6B solution

Step 1:

First, install the mysql library by running:

npm install mysql

Step 2:

Now, create a Node.js file, let's name it selectStudents.js:

// selectStudents.js:
const mysql = require('mysql');

// Create a connection to the MySQL database
const connection = mysql.createConnection({
host: 'your_database_host',
user: 'your_database_user',
password: 'your_database_password',
database: 'your_database_name'
});

// Connect to the database
connection.connect();

// Select all records from the "student" table
connection.query('SELECT * FROM student', (selectErr, results) => {
if (selectErr) {
console.error('Error selecting records:', selectErr);
connection.end();
return;
}

// Display all records on the console
console.log('All records from "student" table:', results);

// Close the database connection
connection.end();
});

No comments:

Post a Comment