Getting data from SQL and weird chars

In last couple days im getting nervous about one thing. I would like to get data from SQL tables (which was created by wordpress). Everything seems to be allright but there is one issue with chars encoding. Its showing strange hashes instead of polish letters. I have tried everything.
-head section contains meta charset utf-8
-php file was saved in utf-8 encoding
-sql tables’ collade set to utf8_general_ci
Heres my site http://wwepolska.pl/m/
And here is my code:

<head><meta charset="utf-8"></head>
<body>
<?php
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES utf-8");
$conn = new mysqli('localhost', 'database', 'password', 'etc');
mysql_query ("set character_set_client='utf-8'"); 
mysql_query ("set character_set_results='utf-8'"); 

mysql_query ("set collation_connection='utf-8'");
if ($conn->connect_error) {
die("error: " . $conn->connect_error);
} 
$sql = "SELECT * FROM `wwepl_posts` WHERE post_status = 'publish' ORDER BY `wwepl_posts`.`post_date` DESC LIMIT 12;";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    echo $row["post_title"]. " " . $row["post_content"]. "<br>";
}
} else {
echo "404";
}
$conn->close();
?>    
</body>

Can you guys help me somehow? I have no idea what to do :/

Related posts

1 comment

  1. Your code should looks like this:

    <?php header('Content-Type: text/html; charset=utf-8'); ?>
    
    <head><meta charset="utf-8"></head>
    <body>
      <?
      $conn = new mysqli('localhost', 'database', 'password', 'etc');      
    
      if ($conn->connect_error) {
        die("error: " . $conn->connect_error);
      } 
    
      $conn->query("SET CHARACTER SET utf8");
    
      $sql = "SELECT * FROM `wwepl_posts` WHERE post_status = 'publish' ORDER BY `wwepl_posts`.`post_date` DESC LIMIT 12;";
      $result = $conn->query($sql);
    
      if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo $row["post_title"]. " " . $row["post_content"]. "<br>";
        }
      } else {
        echo "404";
      }
    
      $conn->close();
    
      ?>    
    </body>
    

    First of all you have to send all headers before you print someting on output (even white characters).

    Also, if you are opening connection using mysqli, then you have to use $conn->query() instead of mysql_query() otherwise it will have no efect & should throw an error.

Comments are closed.