我有一个 MySQL 数据库,其中包含带重音的西类牙语单词,例如 andreá。
我正在使用这个,它显示得很好:
<?php
include "../BD/conexion.php";
mysql_query('SET NAMES utf8');
class Faq
{
public $pregunta;
public $respuesta;
public function __construct($pregunta,$respuesta)
{
$this->pregunta = $pregunta;
$this->respuesta = $respuesta;
}
public static function get()
{
$rows = array();
$res = mysql_query('SELECT * FROM Faq');
while ($row = mysql_fetch_array($res)) {
$rows[] = $row;
}
function filter(&$value) {
$value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
array_walk_recursive($rows, "filter");
return $rows;
}
当我在 html 中打印行时,它显示得很好,如下所示:
但我试图更改为新的 mysqli_ 函数,如下所示:
<?php
$conexion = mysqli_connect("localhost","root","","prueba2");
mysqli_query($conexion,'SET NAMES utf8');
mysqli_close($conexion);
class Faq
{
public $pregunta;
public $respuesta;
public function __construct($pregunta,$respuesta)
{
$this->pregunta = $pregunta;
$this->respuesta = $respuesta;
}
public static function get()
{
$conexion = mysqli_connect("localhost","root","","prueba2");
$rows = array();
$res = mysqli_query($conexion,'SELECT * FROM Faq');
while ($row = mysqli_fetch_array($res)) {
$rows[] = $row;
}
function filter(&$value) {
$value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
array_walk_recursive($rows, "filter");
return $rows;
mysqli_close($conexion);
return $rows;
}
当我展示它时,我得到:
我尝试使用 mysqli_set_charset($conexion, "utf8")
但它有相同的输出... help =(
最佳答案
我相信,如果您在执行任何其他查询之前运行查询来设置名称,它应该可以工作。不过有点在黑暗中拍摄!
如下所示:http://php.net/manual/en/mysqli.query.php#44707
<?php
$mysqli = new mysqli("localhost","root","","prueba2");
$mysqli->query("SET NAMES 'utf8'");
$q = $mysqli->query("SELECT * FROM Faq");
?>
关于php - 使用 htmlspecialchars() 和使用 mysqli 时不显示重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19431377/