Good I have the following problem when bringing information from the database, I am making an AJAX request when loading the DOM so that it brings me all the videos from the database and shows them on the homepage. For this I have the following code in JS
$(document).ready(function() {
$.ajax({
url: "ajax/api.php?accion=videos",
method: "get",
dataType: "json",
success: function(respuesta){
for(var i=0; i<respuesta.length; i++){
$("#videos-index").append(
'<div class="col-xl-3 col-lg-3 col-md-4 col-sm-6 col-12">'+
'<a href="watch/video-watch.php?id="'+respuesta[i].codigo_video+' class="d-block">'+
'<div class="video-miniatura">'+
'<img class="img-fluid" id="miniatura" src='+respuesta[i].url_miniatura+'>'+
'<div class="descripcion">'+
'<a href="watch/video-watch.php?id='+respuesta[i].codigo_video+'" class="video-title" id="titulo-video">'+
respuesta[i].titulo+
'</a>'+
'<p class="yt-color video-text" id="informacion">'+
respuesta[i].nombre_canal+'<br>'+
respuesta[i].num_visualizaciones+'<br>'+
respuesta[i].fecha_subida+
'</p>'+
'</div>'+
'</div>'+
'</a>'+
'</div>'
);
}
},
error: function(e,textStatus, error){
console.log(e);
console.log(error);
console.log(textStatus);
}
});
});
This petition calls api.php which is the following:
include("../class/class-conexion.php");
include("../class/class-videos.php");
$conexion = new Conexion();
switch($_GET['accion']){
case "videos":
echo Videos::obtenerVideos($conexion);
break;
}
$conexion->cerrarConexion();
The method of obtaining Videos is in the videos class defined as follows:
public static function obtenerVideos($conexion){
$sql ="SELECT a.codigo_video ,a.titulo ,a.num_visualizaciones, a.fecha_subida, a.url_miniatura, b.nombre_canal
FROM tbl_videos a
INNER JOIN tbl_canales b ON (a.codigo_canal = b.codigo_canal)";
$result = $conexion->ejecutarConsulta($sql);
$videos = array();
while($fila = $conexion->obtenerFila($result)){
$videos[] = $fila;
}
return json_encode($videos);
}
I made a separate file to corroborate that the SQL statement was correct and if it brings me the information (in the DB there are 15 records for which you should bring me all) the problem is in when I return the json object to print it with the structure that I must have that error, and I was reviewing the 3 files and it could not be. Any help that could be?