0

I have a table in my database called pages, encoded in utf8_unicode_ci (with id, title, template and content columns).

When I inserted hebrew text to the database using phpMyAdmin it worked fine and I saw Hebrew text. But when I insert rows from a php file it inserts gibberish to the database (בלה instead of מכבי and גכדדגכ instead of תל אביב.

These are my files:

index.php

<html>
<head>
    <meta charset="UTF-8" />
</head>
<body>
    <?php
        include "includes/class.database.php";
        include "includes/class.page.php";

        $db = Database::getInstance("localhost", "dsite", "root", "");

        $page = new Page();
        $page->setTitle("מכבי");
        $page->setTemplate("one");
        $page->setContent("תל אביב");
        $page->save();
        $page->setTitle("בלה");
        $page->setContent("גכדדגכ");
        $page->save();
    ?>
</body>

class.database.php

<?php
class Database
{
    private $connection;

    private static $instance = null;

    private function __construct($host = "", $name = "", $username = "", $password = "")
    {
        $this->connection = new mysqli($host, $username, $password, $name);

        if ($this->connection->connect_errno > 0)
            die ($this->connection->connect_error);
    }

    public static function getInstance($host = "", $name = "", $username = "", $password = "")
    {
        if (!self::$instance)
            self::$instance = new Database($host, $name, $username, $password);

        return self::$instance;
    }

    public function getConnection()
    {
        return $this->connection;
    }

    public function getLastId()
    {
        return $this->connection->insert_id;
    }

    public function query($q)
    {
        return $this->connection->query($q);
    }
}

class.page.php

<?php
class Page
{
    private $id;
    private $title;
    private $template;
    private $content;

    private $db;

    public function __construct($pageID = 0)
    {
        $this->db = Database::getInstance();

        if ($pageID > 0)
        {
            $selectPage = $this->db->query("SELECT * FROM `pages` WHERE `id`='$pageID'");
            if ($selectPage->num_rows > 0)
            {
                $page = $selectPage->fetch_assoc();

                $this->id = $page['id'];
                $this->title = $page['title'];
                $this->template = $page['template'];
                $this->content = $page['content'];
            }           
        }
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTemplate($template)
    {
        $this->template = $template;
    }

    public function getTemplate()
    {
        return $this->template;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function save()
    {
        if ($this->id == 0)
        {
            $this->db->query("INSERT INTO `pages` (`title`,`template`,`content`) VALUES ('{$this->title}','{$this->template}','{$this->content}')");
            $this->id = $this->db->getLastId();
        }
        else
        {
            $this->db->query("UPDATE `pages` SET `title`='{$this->title}', `template`='{$this->template}', `content`='{$this->content}' WHERE `id`='{$this->id}'");
        }
    }
}

All files created using Notepad++, encoding set to encode in UTF-8 without BOM.

Naxon
  • 1,354
  • 4
  • 20
  • 40

1 Answers1

1

Try to set the charset in the __construct() of your class.database.php like this:

$this->connection->set_charset("utf8")

http://php.net/manual/de/mysqli.set-charset.php

tino.codes
  • 1,512
  • 1
  • 12
  • 23