0

TBH, feeling so noob... I know I'm making a few mistakes, but I've spent a considerable amount of time already on the connection between MySQL and Nodejs and I'm still unable to figure out the problem. A solution will be appreciated!

I'm using cPanel (with nodejs all setup), and have the following two files in the same directory:

index.html:

<!DOCTYPE html>
<html>
<body>

<h1>MySQL and Node.js</h1>

<script src="config_db.js"></script>
<script>
con.query('SELECT * FROM Index', (err,rows) => {
  if(err) throw err;
  document.write('Data received from Db:\n');
  document.write(rows);
});

</script>
</body>
</html>

config_db.js

var mysql = require("mysql");
var con = mysql.createConnection({
     host: 'localhost',
     user: 'username_here',
     password: 'password_here',
     database: 'database_name_here'
});

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

In the output, I get nothing except for "MySQL and Node.js".

Zuberi
  • 35
  • 6
  • console.log outputs go on the console, while html output goes on your browser window. You said you can see the h1 heading only. Are you looking at the right place ? – Abhishek Ranjan Dec 01 '19 at 19:48
  • I'm sorry that I forgot to mention that document.write is returning the same result as well. – Zuberi Dec 01 '19 at 19:56
  • 1
    I think you're mixing server-side code with client-side code, or that I'm confusing myself. :/ Are you able to run the `node` command? In any case, you're configuring your database connection in the client side, which is not secure for actual use (just saying). – ionizer Dec 01 '19 at 20:04
  • @ionizer `node` seems to be working fine; and yes, it may not be very secure to have a database establishment right in the client-side, but I'm fine with it as of now as security isn't a real priority at this point. – Zuberi Dec 01 '19 at 20:14
  • 1
    Make the title of your post describe your question/problem. "MySQL + NodeJS + HTML" tells nobody anything useful. – Lightness Races in Orbit Dec 01 '19 at 20:15
  • You do need Node when you want to create an actual backend for your HTMLs. But it seems like you don't actually need `node` at all if you're connecting to db from the HTML side (which is client-sided and uses plain Javascript instead of NodeJS). If this is the case, unfortunately, I can't really help. – ionizer Dec 01 '19 at 20:18
  • Still not very descriptive, is it? "doesn't seem to be working" Did you do some debugging to narrow down the problem to _one_ technology? Three technologies is a lot. – Lightness Races in Orbit Dec 01 '19 at 20:18
  • 1
    mysql lib is server-side, what your doing is not possible.. dupe of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Lawrence Cherone Dec 01 '19 at 20:33

1 Answers1

1

Output 'MySQL and Node.js'

is the HTML view which is perfect.

You are using 'console.log' statements for the result of query you make. Simply open browser console developer tool to see the output . (Press F12 in case you are using windows)

Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
  • I'm sorry that I forgot to mention that document.write is returning the same result as well. – Zuberi Dec 01 '19 at 19:56