【问题标题】:I keep getting these special character symbols: ������我不断收到这些特殊字符符号:������
【发布时间】:2020-09-07 19:40:32
【问题描述】:

我不断收到这些特殊字符符号:Bel�n。这些信息来自我的数据库。

我已经在各处添加了<meta charset="utf-8">,但它们似乎没有改变。

我猜这个错误与数据库连接有关。这是我的数据库连接代码,但我不知道在哪里添加字符集或如何添加。

namespace App\Classes\Model;
use PDO;
use PDOException;

class Database
{
    public $databaseHandler;
    public $host = "localhost";
    public $dbname = "database";
    public $user = "root";
    public $password = "";

    public function __construct()
    {
        try {
            $this->databaseHandler = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
            //echo "Database Connected";
        }
        catch(PDOException $error){
            echo $error->getMessage();
        }
    }
}

【问题讨论】:

    标签: php database utf-8 character-encoding special-characters


    【解决方案1】:

    这是您设置字符集的方式(utf8 或任何您需要的)

    $this->databaseHandler = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=utf8", $this->user, $this->password);
    

    【讨论】:

      【解决方案2】:

      我认为你也可以添加以下行

      $this->databaseHandler->exec("set names utf8");
      

      【讨论】:

        猜你喜欢
        • 2021-04-18
        • 2017-10-02
        • 2016-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多