Problem with accented words coming from the MySql database

0

I am using this query to show data coming from a moodle database, but words that have accents are shown with a question mark, I already used two charset types utf-8 and ISO-8859-1 but the error continues, follow image and code:

  

IMAGE

  

CODEOFCONSULTATION

<?php//definiçõesdehost,database,usuárioesenha$host="localhost";
$db   = "bitnami_moodle";
$user = "root";
$pass = "";
// conecta ao banco de dados
$con = mysql_pconnect($host, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR); 
// seleciona a base de dados em que vamos trabalhar
mysql_select_db($db, $con);
// cria a instrução SQL que vai selecionar os dados
$query = sprintf("SELECT name, questiontext FROM mdl_question");
// executa a query
$dados = mysql_query($query, $con) or die(mysql_error());
// transforma os dados em um array
$linha = mysql_fetch_assoc($dados);
// calcula quantos dados retornaram
$total = mysql_num_rows($dados);
?>

<html>

    <head>

    <title>Exemplo</title>
</head>
<body>
<?php

    // se o número de resultados for maior que zero, mostra os dados
    if($total > 0) {
        // inicia o loop que vai mostrar todos os dados
        do {
?>
            <p><?=$linha['name']?> </p> <?=$linha['questiontext']?></p>
<?php
        // finaliza o loop que vai mostrar os dados
        }while($linha = mysql_fetch_assoc($dados));
    // fim do if 
    }
?>
</body>
</html>
<?php
// tira o resultado da busca da memória
mysql_free_result($dados);
?>
    
asked by anonymous 24.07.2018 / 21:47

2 answers

1

In your connection file, do this:

Object-oriented mode:

$mysqli = new mysqli("host", "user," "senha", "db");

$mysqli->query("SET NAMES 'utf8'");
$mysqli->query('SET character_set_connection=utf8');
$mysqli->query('SET character_set_client=utf8');
$mysqli->query('SET character_set_results=utf8');

Procedural:

mysql_query("SET NAMES 'utf8'", $con);
mysql_query('SET character_set_connection=utf8', $con);
mysql_query('SET character_set_client=utf8', $con);
mysql_query('SET character_set_results=utf8', $con);

This will force utf-8 in all cases, both in submissions and in responses.

You're using mysql , it's already a deprecated technology, so I recommend you take a look at mysqli as I did in the object-oriented example.

    
24.07.2018 / 22:02
0

You have to use the characters in utf8, thus saying that the characters contained in the searches cover the whole Western symbol.

<?php header("Content-type: text/html; charset=utf-8"); ?>

put this at the top of the page or do not connect.

    
24.07.2018 / 22:11