【问题标题】:the problem to save Persian data in MySQL database在 MySQL 数据库中保存波斯语数据的问题
【发布时间】:2019-06-20 00:00:06
【问题描述】:

我正在尝试将波斯语数据保存在 MySQL 中。但是当保存在数据库中时,如下图所示。我为数据库中的排序规则设置了 utf8 通用 ci,还添加了 header('Content-Type: text/html; charset=utf-8');在 PHP 文件中。但显示像这张图片一样的数据。

Users.php

public function updateuser(){

if(isset($_GET['phone']) && isset($_GET['fullname']) && isset($_GET['userPhone']) && isset($_GET['citycode']) && isset($_GET['useraddress']) && isset($_GET['userstatus'])){
    $phone = $_GET['phone'];
    $fullname = $_GET['fullname'];
    $userPhone = $_GET['userPhone'];
    $citycode = $_GET['citycode'];
    $useraddress = $_GET['useraddress'];
    $userstatus = $_GET['userstatus'];

$query = "CALL sp_update_user('$phone','$fullname','$userPhone',$citycode,'$useraddress',$userstatus)";}

$dbcontroller = new DBController();

$this->users = $dbcontroller->executeupdateuserQuery($query);
return $this->users;
}

DBController.php

function executeupdateuserQuery($query) {

mysqli_set_charset($conn,'utf8');    
$result = mysqli_query($this->conn,$query);

if ($result)
{
    return "user_updated";
}else{
    return "failed_updated";
}       

}

【问题讨论】:

标签: php mysql


【解决方案1】:

您插入的数据应采用 utf8 编码。您可以使用 utf8_encode 函数对数据进行编码。例如:

$fullname = utf8_encode($_GET['fullname']);

【讨论】:

  • 这是完全错误的。 ISO-8859-1是拉丁文字编码,根本无法存储波斯文字。
【解决方案2】:

当你运行mysql connect时,你必须设置

   mysql_query("SET NAMES 'utf8'");

对于 mysqli

$mysqli->set_charset("utf8")

完整示例:

$connect = mysql_connect($this->hosttype,$this->dbusername,$this->dbpassword)or die('error');
mysql_query("SET NAMES 'utf8'");
$select = mysql_select_db($this->dbname,$connect)or die('error');

【讨论】:

  • $mysqli->set_charset()mysqli_set_charset() 完全相同,您根本不需要 SET NAMES 'utf8'
猜你喜欢
  • 2019-04-14
  • 2013-12-29
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 2015-01-12
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多