php - 使用 PDO 设置 utf8 为 mysql 连接

标签 php mysql pdo utf-8

我这里有一个大问题。我的 mysql 表的许多行都强调了数据,当我使用 javascript 获取这些信息时,它返回 FATAL ERROR 200。我想知道是否可以获取 UTF8 格式的所有数据并将其设置在 MySQL 连接上。 这是我的连接文件

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class dbconfig {
    protected static $host = "";
    protected static $username = "";
    protected static $password = "";
    protected static $dbname = "";
    static $con;

    function __construct() {
        self::$con = self::connect(); 
    }

    protected static function connect() {
        try {
            $link = mysqli_connect(self::$host, self::$username, self::$password, self::$dbname); 
            if(!$link) {
                throw new exception(mysqli_error($link));
            }
            return $link;
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
        } 
    }

    public static function close() {
        mysqli_close(self::$con);
    }

    public static function run($query) {
        try {
            if(empty($query) && !isset($query)) {
                throw new exception("Query string is not set.");
            }
            $result = mysqli_query(self::$con, $query);
            self::close();
            return $result;
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
        }
    } 
}

最佳答案

解决方案

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class dbconfig {
    protected static $host = "";
    protected static $username = "";
    protected static $password = "";
    protected static $dbname = "";
    static $con;

    function __construct() {
        self::$con = self::connect(); 
    }

    protected static function connect() {
        try {
            $link = mysqli_connect(self::$host, self::$username, self::$password, self::$dbname); 
            mysqli_set_charset($link,"utf8");
            if(!$link) {
                throw new exception(mysqli_error($link));
            }
            return $link;
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
        } 
    }

    public static function close() {
        mysqli_close(self::$con);
    }

    public static function run($query) {
        try {
            if(empty($query) && !isset($query)) {
                throw new exception("Query string is not set.");
            }
            $result = mysqli_query(self::$con, $query);
            self::close();
            return $result;
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
        }
    } 
}

关于php - 使用 PDO 设置 utf8 为 mysql 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772170/

相关文章:

php - 电子邮件确认后如何将用户添加到数据库?

MySQL CAST 确实需要它的甜蜜时光

mysql - 评估 WHERE AND drupal 6 mysql 查询

mysql - 在 mySQL 中,如何为多个表中的每个客户端 ID 选择最新的时间戳?

php - 从数组获取 ID 并 POST 到 JOIN 表

php - pdo 代码中 undefined variable

php - Symfony - 自定义验证器和依赖注入(inject)

php - 如何在 Swift 或 Php 中将核心数据编码为 JSON(不带斜线)

javascript - 数据表+列搜索+整体搜索协同工作+服务器端处理

php - 从 MySQL 动态获取特定内容