var Client = require(‘mysql').Client,client = new Client(), Client is not a function.
运行mysql.createClient出错的原因在于,新版的Node.js中的mysql,已经去掉了createClient,而改为了mysql.createConnection。
所以换为mysql.createConnection,就可以正常运行了。
代码如下:var connection = require('mysql').createConnection({
host:"localhost",
user:'root',
password:'123456',
database: 'nodejs_mysql_test'
});
connection.query('USE nodejs_mysql_test');
//查询,并设置回调函数
connection.query(
'SELECT * FROM test',
function selectCb(err, results, fields) {
if (err) {
throw err;
}
console.log("out putting result...");
console.log(results);
for(var i=0;i<results.length;i++)
{
console.log(results[i].id);
console.log(results[i].title);
console.log(results[i].text);
}
//consol