I have created a configuration file, which I include in each file created. This configuration file need to use for connect to the database without declaring it in each file. The problem is that when I pass the data into a table using a query, the encoding is corrupted. I have seen several online solutions, but there would be a way to integrate the fix in the configuration file? In this way do not have to set the type of coding before each query. If I insert: "This is a test", is entered in the database as: "This è a test".
This is my configuration file
define("HOST", "localhost");
define("USER", "root");
define("DATABASE", "test");
define("PASSWORD", "");
define("SET NAMES 'utf8'"); #> you can do something like that?
do u have unicode characters in your table? "SET NAMES 'utf8'" is needed whenever you want to send data to the server having characters that cannot be represented in pure ASCII, like 'ñ' or 'ö'.
That if the MySQL instance is not configured to expect UTF-8 encoding by default from client connections (many are, depending on your location and platform.)
Read http://www.joelonsoftware.com/articles/Unicode.html in case you aren't aware how Unicode works.
If u're not sure, try removing the last line from your config file and give it a try.
The first thing is that you need to run this query. However it's not a very good idea to put query into define and also this define doesn't have any name in the code you showed us.
So you could do it this way:
define("DB_ENCODING", 'utf8');
and then after connecting with your database run query:
"SET NAMES '".DB_ENCODING."'"
However it's still not the best option. If you use mysqli for instance you can use mysqli_set_charset()
instead so in fact you could use:
mysqli_set_charset($con, DB_ENCODING);
Just run the query SET NAMES 'UTF8'; in class constructor after connection to database is made. And yet if you still somewhere have problem with encoding, write a function which runs set names query, so you can easily call it whenever you need.