我正在尝试连接到MYSQL数据库。
我的代码:
E-mail: <input type="email" id="email" size=25><br>
<button id="go" onclick="register()">Go</button>
<script type="text/javascript">
function register(){
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
}
</script>当我打开控制台时,它显示:
Uncaught ReferenceError: require is not defined我安装了nodejs,并使用以下命令从cmd安装了MYSQL:
npm install mysql这里怎么了?
发布于 2021-01-03 03:55:10
客户端上不存在require (至少本机不存在)。您所拥有的代码将在服务器(node.js)上运行。
尝试将其放入一个文件中,比如main.js,然后运行node main.js。
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});https://stackoverflow.com/questions/65546431
复制相似问题