我这里有一个大问题。我的 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/