0

I am trying to query a database on my website's hosting service (HostWinds). However, when I run the following code nothing happens. No error, no console logs, nothing.

I found this code from W3 schools guide on MySQL and Node.js.

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "webmaxlabs.com",
  user: "abc",
  password: "123",
  database: "USER_wp4"
});

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

I have MySQL installed, and I'm using the same username and password I use to login to HostWinds. I am also pretty sure I have the right database name. Is my host misnamed? I am not really sure what to use since 'localhost' can't be correct, right? WebMaxLabs.com is listed as my 'Main Domain' in my cPanel. So I figured it might be my host, but I am really not sure. My

Any feedback or information would be very much appreciated. Thank you!

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43

1 Answers1

0

const express = require('express');
const mysql = require("mysql");
const app = express()


var con = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'test'
});

con.connect(function (err) {
    if (err) {
        console.log('Error connecting to Db');
        return;
    }
    console.log('Connection established');
});


app.get("/", (req, res) => {
    const sqlInsert = "INSERT INTO movie (movie_name,movie_review) VALUES ('inception','good movie')";
    con.query(sqlInsert, (error, result) => {
        res.send("Data send to the Database")
    })

})

app.listen(3001, () => {
    console.log("running on port 3001")
})
Malith Ileperuma
  • 926
  • 11
  • 27