David Xu
David Xu

Reputation: 5597

NodeJS - MySQL and Unicode strings being garbeled

I have a NodeJS server running as a message forwarding service and I've noticed that although it works perfectly with english (ASCII) text, it fails to insert UTF-8 encoded strings into the MySQL database (using nodejs-mysql driver)

Things i've tried:

conn.query("INSERT INTO messages(content) VALUES("+mysql.escape(content)+")", function(err, result) {
    // ...
}

conn.query("INSERT INTO messages(content) VALUES(?)", [content], function(err, result) {
    // ...
}

Both don't work, they end up inserting strings like "???????" into the database.

The table is using latin1_swedish_ci as its default collation and InnoDB as the engine.

This isn't a database (MySQL) issue because I have the same code in PHP and it works fine (using PHP PDO)

This isn't a NodeJS (v8) issue either because the message is correctly forwarded in UTF-8 form, it's just that when it gets sent to the database to be stored it gets stored as a string like "????????" for example.

Example of garbled row:

enter image description here

Does anyone have any idea what could be wrong?

Thanks in advance.

Upvotes: 1

Views: 2312

Answers (1)

wilsotc
wilsotc

Reputation: 800

The latin1_swedish_ci database table can't natively handle UTF-8. It can store UTF-8 in an encoded format which is how your PHP-PDO is working.

Upvotes: 1

Related Questions