Sharp word problem from Mysql database

Asked

Viewed 842 times

0

I’m using this query to show data coming from a Moodle database, but the words that have accents are shown with a query, I’ve used two types of charset utf-8 and ISO-8859-1 but the error continues, follows image and code:

IMAGE

inserir a descrição da imagem aqui

CONSULTATION CODE

<?php
// definições de host, database, usuário e senha
$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);
?>
  • Try inserting at the beginning of the page. <?php header("Content-type: text/html; charset=utf-8"); ?>

  • Words in the database are marked correctly or are being saved with special characters?

  • @Thiagomagalhães In the bank are accented correctly

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.

Are you using the mysql, is already a deprecated technology, I recommend you take a look at mysqli as I did in the object-oriented example.

  • I could tell you what it would look like in my code above?

  • 1

    Oh yes, you put the connection part, I’ll put it here, just a moment

  • @Miguelsilva take a look now

  • Worked perfectly!

  • 1

    @Miguelsilva You’re welcome, partner ;)

0

You have to use the characters in utf8, saying that the characters contained in the searches cover the entirety of the western symbols.

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

put this at the beginning of the page or on the conect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.