Error printing mysql data [duplicate]

1

Good afternoon! I'm listing some mysql data using php; but when displayed in place of words that are accentuated 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 charset meta tag set to utf-8 .

>

This is the function code where I print the query result:

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;
    }
}

}

    
asked by anonymous 28.12.2015 / 17:51

1 answer

2

You can try replacing this

} else {
    return $mysqli;
}

so

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

something else

Check the encoding of ALL your files. Only the charset meta tag is not enough to set the encoding for UTF-8. You must save the files with that encoding. Preferably, UTF-8 without BOM.

    
28.12.2015 / 18:38