简体   繁体   English

Node.js,连接到phpMyAdmin中的数据库

[英]Node.js, connect to database in phpMyAdmin

I am trying to query a database on my website's hosting service (HostWinds).我正在尝试在我的网站托管服务 (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.我从关于 MySQL 和 Node.js 的 W3 学校指南中找到了此代码。

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.我安装了 MySQL,并且我使用的用户名和密码与登录 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?我不确定要使用什么,因为'localhost'不可能是正确的,对吧? WebMaxLabs.com is listed as my 'Main Domain' in my cPanel. WebMaxLabs.com 在我的 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!谢谢!

 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") })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM