Error while printing mysql data

Asked

Viewed 92 times

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 document SEM BOM

  • 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.

  • 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.

  • Just to mention the whole db is in utf8_general_ci.

  • 1

    @Jahnkrauss and the data stored on it?

  • @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.

  • 1

    @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.

  • @Guilhermenascimento does not believe he is a duplicate. The problem was not the coding of the file but the data entered in the database.

  • 1

    Example: AÇÃO in UTF-8: 0x41 0xC3 0x87 0xC3 0x83 0x4F ... AÇÃO in ISO-8859-1: 0x41 0xc7 0xc3 0x4f

  • 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 ;)

  • One more detail, closing questions has nothing to do with negative votes or deleting ;)

  • @Guilhermenascimento and if I do not find the solution there, I should start a new question or ask the same question?

  • Yes new question but must justify, I believe there I have enumerated all problems with ANSI, utf8 without good, mysql api and mysql engine.

Show 8 more comments

1 answer

2


you can try to replace this

} else {
    return $mysqli;
}

therefore

} else {
    $mysqli->set_charset("utf8");
    return $mysqli;
}

something else

Check the encoding of ALL your files. Only the meta tag charset is not enough to set the encoding to UTF-8. You must salvage the files with said encoding. Preferably, UTF-8 without GOOD.

  • 2

    Interestingly, if I save my PHP as ISO-8859-1 in my code editor, how does this interfere with the output of Mysql? I didn’t quite understand that part of your answer.

  • I checked all the files and saved as UTF-8 without GOOD.

  • 1

    Mysql output doesn’t really interfere with anything. But as soon as the server renders the page, it will encounter an encoding conflict, and will try to write UTF-8 where it only accepts ISO-8859-1. And out come those strange characters.

  • @Rafaelgonçalves didn’t make much sense to me the page rendering part you said, but it was more to get an idea if what you said was really what you meant. Grateful for the return.

  • Perhaps you can explain to me better what you meant. I understand this way, but you can have a better explanation than mine. I don’t have much teaching. Hugs!

  • @Rafaelgonçalves is that the comments would be a little long. In theory, the encoding of the page is determined by the header and/or by meta tag, and the content of this would only interfere in the absence of both. Even so, this would be client-side, not rendered by the server. I thought you got a little confused in the answer so I wanted to better understand your statement. Anyway, it runs away a little from the original question, so you couldn’t handle it with much complexity.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.