1
Good afternoon! I am listing some mysql data using php; but when it is displayed in place of the words that are accented I see characters like these . My schema and table are configured with charset utf8_general_ci. And in my html document where the data returned from the database is being displayed, it has the meta tag charset configured for utf-8.
This is the code of the function where I print the result of the query:
public function showTinyNews() {
include_once 'sys/models/Connection.class.php';
$conn = new Connection();
$execQuery = mysqli_query($conn->connect(), "SELECT * FROM news ORDER BY date DESC");
foreach ($execQuery as $news) {
echo "<li>
<h2><a href='index.php?p=news&id={$news['idnews']}'>{$news['title']}</a>
<span>Por: {$news['author']} ({$news['date']})</span></h2>
<p>{$news['news']}</p>
<a class='leia-mais' a href='index.php?p=news&id={$news['idnews']}'>leia mais</a>
</li>";
}
}
Connection code
class Connection {
static $host = 'localhost';
static $user = 'root';
static $password = '00';
static $database = 'database';
public function connect(){
$mysqli = new mysqli(self::$host, self::$user, self::$password, self::$database);
if(mysqli_errno($mysqli)){
echo "Failed to connect to database: mysqli_connect_errno()
mysqli_connect_error()";
} else {
return $mysqli;
}
}
}
try to use
$conn->exec("SET CHARSET UTF-8");
or$conn->set_charset("utf8")
and also save your documentSEM BOM
– RFL
I saved all related documents as utf-8 without good and the error persisted. However $Conn->exec("SET CHARSET UTF-8" and $Conn->set_charset("utf8") only generated an error on the page, in fact it did not only display the Divs. and did not cause any errors. I changed the question with the Connection class.
– Rech
Did you make sure that the data is actually in UTF-8 on DB? The best way is to look at the data in hexadecimal, to make sure. Can’t trust the screen output.
– Bacco
Just to mention the whole db is in utf8_general_ci.
– Rech
@Jahnkrauss and the data stored on it?
– Bacco
@Bacco Good question! With your question I understood why was generating the error. When I entered the data I did it by a simple select and did not set it.
– Rech
@Jahnkrauss the best sure you have of a DB information is to look at it byte by byte. Usually utilities like Mysql Query Browser or Mysql Workbench have a hexadecimal view mode, which is a great way to check the data.
– Bacco
@Guilhermenascimento does not believe he is a duplicate. The problem was not the coding of the file but the data entered in the database.
– Rech
Example:
AÇÃO
in UTF-8:0x41 0xC3 0x87 0xC3 0x83 0x4F
...AÇÃO
in ISO-8859-1:0x41 0xc7 0xc3 0x4f
– Bacco
Understand that many questions are duplicated not because of the questions, but because there is an answer elsewhere that solves the problem. That being the case, the answer there already responds to the problem proposed here. Duplicating questions is not something harmful is just for one question to lead to another where there are other possible solutions, note that the answer here can help you, but this problem can be caused by a number of factors and there is an answer that covers all these factors ;)
– Guilherme Nascimento
One more detail, closing questions has nothing to do with negative votes or deleting ;)
– Guilherme Nascimento
@Guilhermenascimento and if I do not find the solution there, I should start a new question or ask the same question?
– Rech
Yes new question but must justify, I believe there I have enumerated all problems with ANSI, utf8 without good, mysql api and mysql engine.
– Guilherme Nascimento