I have a problem to get data in my language with tis-620
[MySQL Structure]
CREATE TABLE `foo` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`address` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=tis620
[MySQL data]
| id | name | address |
--------------------------------|
| 1 | ทดสอบ | 6/12 บ้านแสนดี |
[NodeJS function]
function getAllFoo() {
return new Promise((resolve, reject) => {
try {
// Retrieving
db.query("SELECT * FROM foo", (err, rows, fields) => {
if(err) { throw err }
resolve(rows)
})
} catch (error) {
reject(error)
}
})
}
[NodeJS API]
const Foo = require('./foo')
app.get('/getFoo', (req, res) => {
Foo.getAllFoo().then((rows) => {
res.header("Content-Type", "application/json; charset=utf-8")
res.status(200).json(rows)
}).catch((err) => {
throw err
})
res.status(200).json(rows);
})
Then, i try request this endpoint /getFoo
but response data is : ��ҹ������
[Response json data]
[
{
"id" : 1,
"name" : "��ҹ.������",
"address" : "��ҹ������ �.� �.������"
}
]
This JSON result i will using with my PHP project
Solution :
[MySQL connection]
- in mysql connection add charset
to tis620 and add "SET NAMES UTF8"
when your database is connected
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret'
charset : 'tis620'
})
connection.connect(function(err) {
if (!err) {
connection.query("SET NAMES UTF8")
console.log('connected as id ' + connection.threadId);
} else {
console.error('error connecting: ' + err.stack);
}
});