2013-10-17 30 views
0

显示我有一个MySQL数据库与西班牙语的单词,例如像andreá口音。重音未与htmlspecialchars()和使用mysqli

我使用这一点,它显示罚款:

<?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, “UTF-8”)

但它具有相同的输出...帮助=(

+0

你试过在同一个页面上同时运行脚本,以确保该问题来自mysqli_而不是别的? –

回答

1

我相信如果您在执行任何其他查询之前运行查询来设置名称,它应该可以工作。虽然在黑暗中有点射击!

正如在这里看到: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"); 
?> 
+0

我已经这样做了,检查我的第一行代码... –

+0

对不起,我应该澄清。我认为你需要在你打开的每个连接中进行。你确实设置了它,但是关闭了这个连接,并且打开了一个新连接。 –

+0

谢谢你现在,如果我在做htmlspecialchars()后转储数组,我得到holáandreá无论如何要做到这一点,而不必每次都调用集合名称? @斯科特Rowell –

0

检查您的表的字符集。如果它不是utf-8,那么它就不会有什么区别,因为它将字符集转换为它在理解它之前将它分解出来。

Use this作为附加信息

+0

谢谢,是的,他们在utf8 –