1

I'm from Mexico, so our strings contain some special characters that need to be stored and later displayed. Is there a way (using PHP) to encode a string from MySQL and use it in html?

  <?php 
    function especialidades($dbcesp){
        $q="SELECT NombreEspecialidad FROM especialidad;";
        $r=mysqli_query($dbcesp, $q);

        while ($esp=mysqli_fetch_assoc($r)) {
            ?>
            <button type="button" class="btn btn-default"><?php echo $esp['NombreEspecialidad']; ?></button>
            <?php
        }
    } 
   ?>

This is the way I retrieve and generate some buttons for my webpage, but for some reason, aren't displayed correctly.

1
  • Please, consider this as well. Commented May 27, 2015 at 7:21

3 Answers 3

3

first go to your phpmyadmin then set Server connection collation to utf8mb4_general_ci

then select your database name then press on your table then press on structure you will see all columns inside your table go and change all columns using type varchar or text then go press on change the change the collation to utf8_general_ci then save then try to input another entry to your database it will be clearly now without any fuzzing words

and the top of your code type

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and those lines in the top of your php code to define the utf charset

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
header("Content-Type: text/html; charset=UTF-8");
Sign up to request clarification or add additional context in comments.

3 Comments

It didn't work :( It doesn't show the original character
you are using phpmyadmin ?
check my answer again !
1

You should configure database for using UTF-8. Also don't forget to set encoding on html to UTF-8 too.

Also, check the following link: MySql spanish character data

Comments

0

I had to change the database collation to utf8_spanish_ci (during database creation in PHPMyAdmin).

Also, I had to add the charset meta tag to the html document:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

And finally, I used the PHP encode api, being used as follows:

  <?php 
function especialidades($dbcesp){
    $q="SELECT * FROM especialidad;";
    $r=mysqli_query($dbcesp, $q);

    while ($esp=mysqli_fetch_assoc($r)) {
        ?>
        <a href="?page=6&esp=<?php echo $esp['CveEspecialidad']; ?>">
        <button type="button" class="btn btn-default" value="<?php echo $esp['CveEspecialidad']; ?>">

                <?php
                    echo utf8_encode($esp['NombreEspecialidad']); 
                ?>

        </button>
        </a>
        <?php
    }
} 
?>

This worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.