Add utf-8 special characters in (like ñ and accents) in mysqli

I have a query to a table in PHP towards MySQL, but the special characters are not displayed, how and where do I add the uft-8? in the connection or in the query?

<?php
$host="localhost";
$user="root";
$password="";
$db="basededatos1";
$con = new mysqli($host,$user,$password,$db);



$sql1= "SELECT * FROM registros";
$query = $con->query($sql1);
?>

<?php if($query->num_rows>0):?>
<table class="table table-bordered table-hover">
<thead>
<th>dato</th>
<th>dato</th>
</thead>
<?php while ($r=$query->fetch_array()):?>
 3
Author: Vicky Vicent, 2016-06-10

2 answers

If you are using xampp you must configure apache and php to use utf8:

In apache:

[httpd.conf]

AddDefaultCharset utf-8

In some apache versions AddDefaultCharset is not found in the usual place and you have to search CF cfg['DefaultCharset'] = "; or similar variables and change it to $cfg['DefaultCharset'] = 'utf-8';

In php:

[php.ini]

default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=0

If you are using a version greater than or equal to php 5.6 these parameters are obsolete:

mbstring.internal_encoding
mbstring.http_input
mbstring.http_output

And just put default_charset = "utf-8"

If you use mysql all database and table collation must be utf8-utf8_spanish_ci and on connection:

$Conex=new mysqli(.....);
$Conex->set_charset("utf8");

Finally your html must also configure the utf8 with the following line:

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

If you use external libraries or APIs they must also configure UTF-8 because that with something missing will cause you to have character problems.

If I forgot, also set your IDE or text editor to UTF-8.

 2
Author: abrahamhs, 2016-06-10 18:07:14

You can set the character set when you are querying, setting the character set to UTF8 i.e.; you could do so;

$con->set_charset("utf8"); //Estableciendo utf8 $sql1= "SELECT * FROM registros"; $query = $con->query($sql1);

 1
Author: Alfonso Carrasco, 2016-06-10 17:51:55