-1

I have a database class but when I use it values from the database are printed without polish chars :(

Gra�yna

it should be

Grażyna

In mysql database Collation is utf8_polish_ci

How can I solve this problem?

    <?php 

class Baza {

    private $link;
    private $host, $username, $password, $database;

    public function __construct($host, $username, $password, $database){
        $this->host        = $host;
        $this->username    = $username;
        $this->password    = $password;
        $this->database    = $database;

        $this->link = mysql_connect($this->host, $this->username, $this->password)
            OR die("Problem z połączeniem z bazą.");

        mysql_select_db($this->database, $this->link)
            OR die("Problem z wyborem bazy.");

        return true;
    }

    public function query($query) {
        $result = mysql_query($query);
        if (!$result) die('Niepoprawne zapytanie: ' . mysql_error());
        return $result;
    }

    public function __destruct() {
        mysql_close($this->link)
            OR die("Problem z odłączeniem się!");
    }


}
?>

2 Answers2

0

You should use SET NAMES utf8 after connecting and utf-8 without BOM text files for your code and content-type: x; charset=utf-8 response and request headers even with ajax.

Btw. your should really use PDO or mysqli with prepared statements instead of your current mysql driver.

Community
  • 1
  • 1
inf3rno
  • 24,976
  • 11
  • 115
  • 197
0

First of all. Stop using mysql_* it's deprecated, switch to PDO.

Answer

Use mysql_set_charset to set proper encoding.

mysql_set_charset('utf8')

In your case

$this->link = mysql_connect($this->host, $this->username, $this->password)
            OR die("Problem z połączeniem z bazą.");
mysql_set_charset('utf8', $this->link)
mleko
  • 11,650
  • 6
  • 50
  • 71