php - Characters appear as gibberish in mysql (Hebrew) -
this question has answer here:
- utf-8 way through 14 answers
my charset in database set utf8_unicode_ci, all files encoded in utf8 (without bom).
here php code:
<?php require_once("./includes/config.php"); $article = new article(); $fields = array( 'status' => '0', 'title' => 'מכבי ת"א אלופת אירופה בפעם ה-9', 'shorttitle' => 'מכבי ת"א אלופת אירופה', 'priority' => '1', 'type' => '1', 'category' => '2', 'template' => '68', 'author' => '1', 'date' => date("y-m-d h:i"), 'lastupdate' => date("y-m-d h:i"), 'preview' => 'בלה בלה בלה', 'content' => 'עוד קצת בלה בלה בלה', 'tags' => 'מכבי ת"א,יורוליג,אליפות אירופה', 'comments' => '1' ); $article->set($fields); $article->save();
for reason, hebrew characters appear in phpmyadmin:
מכבי ת"× ×לופת ×ירופה ×‘×¤×¢× ×”-9
database connection code:
<?php final class database { protected $fields; protected $con; public function __construct($host = "", $name = "", $username = "", $password = "") { if ($host == "") { global $config; $this->fields = array( 'dbhost' => $config['database']['host'], 'dbname' => $config['database']['name'], 'dbusername' => $config['database']['username'], 'dbpassword' => $config['database']['password'] ); $this->con = new mysqli($this->fields['dbhost'], $this->fields['dbusername'], $this->fields['dbpassword'], $this->fields['dbname']); if ($this->con->connect_errno > 0) die("<b>database connection error:</b> ".$this->con->connect_error); } else { $this->con = new mysqli($host, $username, $password, $name); if ($this->con->connect_errno > 0) die("<b>database connection error:</b> ".$this->con->connect_error); } }
any ideas why?
you have set database's , file's character set utf-8, data transfer between php , database needs set correctly.
you can using set_charset:
sets default character set used when sending data , database server.
add following last statement of database constructor:
$this->con->set_charset("utf8");
this not fix issue data in database, new data written database should notice difference.
if decide rebuild database, please consider using superior utf8mb4 character set, described in mysql docs:
the character set named utf8 uses maximum of 3 bytes per character , contains bmp characters. of mysql 5.5.3, utf8mb4 character set uses maximum of 4 bytes per character supports supplemental characters:
for bmp character, utf8 , utf8mb4 have identical storage characteristics: same code values, same encoding, same length.
for supplementary character, utf8 cannot store character @ all, while utf8mb4 requires 4 bytes store it. since utf8 cannot store character @ all, not have supplementary characters in utf8 columns , need not worry converting characters or losing data when upgrading utf8 data older versions of mysql.
utf8mb4 superset of utf8
Comments
Post a Comment