Gurpinder Gurpinder - 1 month ago 9
Node.js Question

Arabic Text In MySQL Table?

I have a small problem. Mysql supports arabic right. When i store data in tables it's alright and everything works fine, but when i issue an sql to retrieve data. I get the arabic text as ???? marks. I have changed the charset of the tables and of the columns but still the same problem. I am using node express framework.

Answer

Here is the connection file of node withe express framework

 var mysql = require('mysql');
 var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: '',
charset: 'utf8'
 };
 function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since

// the old one cannot be reused.
connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
        console.log('error when connecting to db:', err);
        setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
    }                                     // to avoid a hot loop, and to allow our node script to
});                                     // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
        handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
        throw err;                                  // server variable configures this)
    }
});
}
 handleDisconnect();
 var sql1 = "SET CHARACTER SET utf8";
 connection.query(sql1, function (err, result) {
 var sql = "SET SESSION collation_connection ='utf8_general_ci";
 connection.query(sql,  function (err, result) {
 });
 });
Comments