I am trying to display an image taken from the mysql server to display it on my website by generating an xml and creating a table with javascript.
I have a getNoticia.php where I communicate with the server:
<?php
// Configuración BASE DE DATOS MYSQL
$servidor = "localhost";
$basedatos = "BDNoticias";
$usuario = "root";
$password = "";
// Creamos la conexión al servidor.
$conexion = mysqli_connect($servidor, $usuario, $password,$basedatos) or die(mysqli_error($conexion));
mysqli_set_charset($conexion,"utf8");
// Consulta SQL para obtener los datos de los centros.
$sql = "SELECT * FROM noticias";
$resultados = mysqli_query($conexion,$sql);
$XML ='<?xml version="1.0" encoding="UTF-8"?>';
$XML .='<datos>';
while ($fila = mysqli_fetch_array($resultados)) {
$XML .='<documento>';
$XML .='<idnotici>'.$fila["idnoticia"].'</idnotici>';
$XML .='<titul>'.$fila["titulo"].'</titul>';
$XML .='<descripcio>'.$fila["descripcion"].'</descripcio>';
$XML .='<auto>'.$fila["autor"].'</auto>';
$XML .='<fech>'.$fila["fecha"].'</fech>';
$XML .='<img>'.base64_encode($fila["imagen"]).'</img>';
$XML .='</documento>';
}
$XML .='</datos>';
// Cabecera de respuesta indicando que el contenido de la respuesta es XML
header("Content-Type: text/xml");
// Para que el navegador no haga cache de los datos devueltos por la página PHP.
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
echo $XML;
mysqli_close($conexion);
?>
If I remove the base64_encode()
from tag I get lots of "?" by console they give me the error and it doesn't show me anything. If I don't remove it, it shows me in the table a very long line of characters that I imagine is the image.
In theory, this line mysqli_set_charset($conexion,"utf8");
is fine, so I understand that the problem comes with the encoding of the image.
Without base64_encode()
:
With base64_encode()
:
To display the data I use this function in js:
function construirListado(oXML)
{
// Crear tabla
var oTabla = document.createElement("table");
oTabla.border = "3";
var oTHead = oTabla.createTHead();
var oFila = oTHead.insertRow(-1);
var oTH = document.createElement("TH");
oTH.textContent = "IDNoticia";
oFila.appendChild(oTH);
oTH = document.createElement("TH");
oTH.textContent = "Título";
oFila.appendChild(oTH);
oTH = document.createElement("TH");
oTH.textContent = "Autor";
oFila.appendChild(oTH);
oTH = document.createElement("TH");
oTH.textContent = "Fecha";
oFila.appendChild(oTH);
/*
oFila = oTHead.insertRow(-1);
oTH = document.createElement("TH");
oTH.textContent = "Descripción";
oFila.appendChild(oTH);*/
var oTBody = oTabla.createTBody();
var oPersonas = oXML.querySelectorAll("documento");
for (var i = 0; i < oPersonas.length; i++) {
oFila = oTBody.insertRow(-1);
var oCelda = oFila.insertCell(-1);
oCelda.textContent = oPersonas[i].querySelector("idnotici").textContent;
oCelda = oFila.insertCell(-1);
oCelda.textContent = (oPersonas[i].querySelector("titul").textContent);
oCelda = oFila.insertCell(-1);
oCelda.textContent = (oPersonas[i].querySelector("auto").textContent);
oCelda = oFila.insertCell(-1);
oCelda.textContent = (oPersonas[i].querySelector("fech").textContent);
oFila = oTBody.insertRow(-1);
oFila = oTBody.insertRow(-1);
oCelda = oFila.insertCell(-1);
// Combina para ocupar toda la fila
oCelda.colspan = 4;
oCelda.textContent = (oPersonas[i].querySelector("descripcio").textContent);
oCelda = oFila.insertCell(-1);
oCelda.textContent = (oPersonas[i].querySelector("img").textContent);
}
document.querySelector("#listado").innerHTML = "";
document.querySelector("#listado").appendChild(oTabla);
}
PHP code
Your code doesn't generate correct XML since you don't use
htmlspecialchars()
when creating XML manually, so any characters that might interfere with the XML format will break it, might corrupt it, or might inject unwanted code into it.Still I suggest you use
DOM
to create XML documents in PHP.Here is an example of how to adapt your code to
DOM
:JavaScript code
Once the XML has been correctly generated, we are ready to read it on the web page using any technology available for it, such as
XMLHttpRequest
orfetch
.Suppose we do it using
fetch
:Now your function will have as input the XML obtained from your PHP script, so it only remains to generate an image from the BASE64 data sent in the XML:
Resources used to reproduce this issue: