Nodejs query mysql database data display garbled code

  node.js, question

Nodejs query mysql database data display garbled code

Run nodejs code with the following results:

D:\Program Files\nodejs>NODE E:\project\test\sqlServer.js
[ RowDataPacket { id: 1, name: ‘ÕÅÈý’, sex: ‘Å®’ },
RowDataPacket { id: 2, name: ‘ÕÅÄÐ’, sex: ‘Å®’ } ]
[ RowDataPacket { id: 1, name: ‘ÕÅÈý’, sex: ‘Å®’ },
RowDataPacket { id: 2, name: ‘ÕÅÄÐ’, sex: ‘Å®’ } ]

Nodejs code is as follows:
var mysql=require(‘mysql’);
var client = mysql.createConnection({
user: ‘root’,
password: ”,
database:’user’,
});
client.connect(function (err) {

if(err)
 Log ('Failed to Establish Connection with mysql Database');

});
client.query(
‘SELECT * FROM userinfo’,
function selectCb(err, results, fields) {

if (err) {
 console.log(err);
 bracket
 else{
 if(results)
 bracket
 for(var i = 0;   i < results.length;  Icaga)
 bracket
 console.log(results);
 bracket
 bracket
 else{
 Log ("failed");
 bracket
 bracket
 client.end();

bracket
);

The mysql database statement is as follows:

create database user character set UTF8;

use user;

create table userinfo
(
id int primary key auto_increment not null,
name varchar(20) not null,
sex varchar(2) not null
);

insert into userinfo
(name,sex)
values
(‘Zhang San’,’ female’);
insert into userinfo
(name,sex)
values
(‘Zhang Nan’,’ Female’);

Results of queries under mysql:

图片描述

How should this problem be solved? Still hope to pass by prawn to give directions.

Try adding a parameter charset to the connection?

var client = mysql.createConnection({
 user: 'root',
 password: '',
 database:'user',
 charset:'UTF8_GENERAL_CI'
 });

Or in the mysqld section of the default mysqld configuration file my.inicharacter-set-server=utf8After that, restart the mysqld service (I am using mariadb, and I am not sure if this configuration can be used in mysql)