php - 使用 htmlspecialchars() 和使用 mysqli 时不显示重音

标签 php html mysql

我有一个 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 中打印行时,它显示得很好,如下所示:

enter image description here

但我试图更改为新的 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;

}

当我展示它时,我得到:

enter image description here

我尝试使用 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/

相关文章:

php - 在 mysql 中排序 VS 已经排序的数组

php - 关于A4尺寸标记和打印问题

php - 无法从 JSON 响应中获取正确的字符串?

php - 在php中将数组转换为json对象

java - 使用 Java 的浏览器小程序中的 Web 浏览器

javascript - 如何在一个网站上使用两个 JavaScript (.js) 文件?

javascript - 刷新页面时计时器会自动刷新

html - 为什么 wordpress 上的导航栏样式不适用?

database - CakePHP 根据同一行中的其他值更新字段值?

mysql - 聊天应用好友列表