Return of "?" instead of the special characters in a SELECT

Asked

Viewed 2,359 times

2

I’ve got a problem, I’ve encoded everything with utf-8, I’ve tried multiple ways to fix it and I can’t, in one <option> of HTML, I pull a table from the database, but it returns with a special character symbol instead of the accents as in the photo below: inserir a descrição da imagem aqui

The code of <select> is ésse

<select id="modalidade" name="modalidade" class="form-control">
        <?php
            header("Content-type:text/html; charset=utf-8");
            //require_once ('../model/Conexao.php');
            //mysql_set_charset('utf8');
            $sql = "SELECT * FROM modalidade";
            $cnx = mysqli_connect("localhost", "root", "", "olimpiada");
            $resultado = mysqli_query($cnx,$sql,MYSQLI_STORE_RESULT);
            $qtde = mysqli_num_rows($resultado);

            if($qtde>0)
            {
                while($linha = mysqli_fetch_array($resultado))
                {

                    echo "<option value=".$linha['idModalidade'].">";
                    echo $linha['modalidade'];
                    echo "</option>";

                }
            }
        ?>
</select>

Here is the database structure inserir a descrição da imagem aqui

Thanks in advance

  • The project is configured as utf-8 as well?

  • Just for the record , o header("Content-type:text/html; charset=utf-8"); should be before any HTML, you put in the middle of the HTML, this does not know recognize, unless you use ob_start (which can be an overuse without need), still it is best to organize the headers above all and before any output.

1 answer

2


Uses the function of PHP utf8_encode(), utf8_encode($linha['modalidade']);

Or change your table in the bank to SET=utf8 COLLATE utf8_unicode_ci

  • Olá Henrique, don’t take it personally, the author of the question already uses utf8mb4, it makes no sense to use SET=utf8 COLLATE utf8_unicode_ci and the use of utf8_encode in this case this more for a patch, it is something that works yes, but still can have other problems, I recommend you to research before, this type of question has been asked at least 20 times, I will cite to you only one example: https://answall.com/a/43205/3635, because the author is having problems with coding, instead of kicking an answer, search if there is already a good answer and mark as duplicate. ;)

  • Dude, I didn’t kick anything. I just told him what I did to fix it. Patch or no patch, solved it. So much so that they accepted the answer. Now, if you have a better answer. Put it there, I will read and I will learn from your answer, the author will probably do the same... Better than trying to teach a lesson with "don’t get me wrong" or "kick back".

  • I’m not teaching you a lesson, I’ve been polite to you, I’ve explained the reason and the problems, I’ve spent time here, the least you can do is try to assume good intentions when someone is trying to guide you instead of thinking it’s a personal attack. As I already said this question of the author is a duplicate and before answering you should have searched the site if there were already similar questions with answers and use the button "Flag" to mark with duplicate. We are all here to learn, you can learn more if you research too ;)

Browser other questions tagged

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