Tengo problemas con los acentos en mi web. Siguiendo este post de este foro, añadí a mi fichero las siguientes líneas:
header('Content-Type: text/html; charset=UTF-8');
utf8_decode($estudiante["id"])
Este sería mi fichero:
<?php
include_once "conexion.php";
include_once "Estudiante.php";
header('Content-Type: text/html; charset=UTF-8');
$estudiantes = Estudiante::obtener();
?>
<style>
h3 {
font-weight: 10;
}
header {
color: #F57B22;
padding-top: 1vw;
padding-bottom: 1vw;
text-align: center;
}
table{
border-collapse: collapse;
margin: 25px 0;
font-size: 0.8em;
font-family: sans-serif;
min-width: 450px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
thead tr {
background-color: #F57B22;
color: #ffffff;
height: 3em;
font-size: 1em;
text-align: middle;
padding: 12px 15px;
border:2px #F57B22;
}
td{
}
td { padding: 12px 15px; }
tbody tr { border-bottom: 1px solid #dddddd; }
tbody tr:nth-of-type(even) { background-color: #f3f3f3; }
tbody tr:last-of-type { border-bottom: 2px solid #F57B22; }
</style>
<div class="row">
<div class="col-12">
<header>
<h1>LISTADO DE ESTUDIANTES</h1>
</header>
</div>
<div class="col-12 table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th><b>ID<b></th>
<th><b>NOMBRE<b></th>
<th><b>GRUPO<b></th>
<th><b>NOTA<b></th>
<th><b>Editar<b></th>
</tr>
</thead>
<tbody>
<?php foreach ($estudiantes as $estudiante) { ?>
<tr>
<td><?php echo utf8_decode($estudiante["id"]) ?></td>
<td><?php echo utf8_decode($estudiante["nombre"]) ?></td>
<td><?php echo utf8_decode($estudiante["grupo"]) ?></td>
<td><?php echo utf8_decode($estudiante["nota"]) ?></td>
<td>
<a href="editar_estudiante.php?id=<?php echo $estudiante["id"] ?>" class="btn btn-warning">
Editar
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php
include_once "pie.php";
La codificación en MySQL es utf8_unicode_ci.
Añado el fichero conexión.php
<?php
$host = "localhost";
$usuario = "_______";
$contrasenia = "______";
$base_de_datos = "____";
$mysqli = new mysqli($host, $usuario, $contrasenia, $base_de_datos);
if ($mysqli->connect_errno) {
echo "Falló la conexión a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
He añadido al fichero conexión la sentencia: $mysqli->set_charset("utf8");
y ahora en vez de salir ? en caracteres especiales, sale �.