0
我在我的數據庫中調用pages
,在utf8_unicode_ci
編碼表(附有id
,title
,template
和content
列)。廢話插入到數據庫,而不是希伯來語
當我使用phpMyAdmin插入希伯來語文本到數據庫時,它工作正常,我看到了希伯來語文本。但是,當我從一個PHP文件中插入行其插入胡言亂語到數據庫(בל×」
而不是מכבי
和×’×›×「×「×’×›
,而不是תל אביב
這些都是我的文件:
的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}'");
}
}
}
所有文件用記事本++,編碼設置爲encode in UTF-8 without BOM
創建。
檢查http://php.net/manual/en/function。 mysql-set-charset.php – vinu
看看[這個問題](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279#279279) – Michel
要麼設置字符集或者運行SET NAMES UTF8作爲第一個查詢 – Mihai