Hello friends, my problem is something weird, I do not know what to do since I'm trying to bring the sum of each set of questions that belong to a single profile, that is: I have 5 questions from profile 1,
with the id: 1,2,3,4 and 5 now I add the values of the field 'respuesta'
that have those id_question, as a final result would have to give me: 5 questions for 8 would be 40, but it does not, it is not yes PHP since when I run the query directly on my phpMyAdmin it works. Please help.
My phpMyAdmin query works:
Aclaro que ahorita en mi código PHP solo estoy realizando una suma ya que si se dan cuenta son 9 sumas ya que son 9 perfiles pero solo estoy haciendo una prueba ya que ni siquiera 1 resultado me sale, por lo menos quiero obtener el primer resultado que les puse como ejemplo :(.
My table to which I am making the query, has 48 rows and the student_id is the same, but the question_id is not, since they do not repeat, that is: they go from 1 to 48 since there are 48 questions and the answer all 8 are I doing wrong? I accept suggestions and corrections, Thanks:
My table questions:
Result of the console.log ();
My ajax code, Aclaro que cuando lo ejecuto todo si funciona, ya que se muestra el alert:
function Obtener_Resultados(){
var request = $.ajax({
url: "Resultados/obtener_resultados_preliminares.php",
type: "POST",
data:{id_alumno: id_estudiante},
dataType:"jsonp",
jsonp:"jsoncallback",
crossDomain: true,
cache: false
});
request.done(function(data){
//alert(JSON.stringify(data));
if(data.estado=="exito"){
//array_resultados_finales = data.resultados;
alert("Exito");
console.log(data.valores);
//console.log(array_resultados_finales);
//destroy_session();
}else{
alert("Error: No se pudieron obtener los resultados");
}
});
request.fail(function(textStatus){
alert("Error en la petición: obtencion de resultados: " + JSON.stringify(textStatus));
});
}
My PHP code:
<?php
//header("Content-Type: text/json");
$datos = array();
$array_resultados = array();
$id_estudiante = $_GET['id_alumno'];
$conexion = new mysqli("localhost", "root", "", "aiup");
$sql = "SELECT SUM(respuesta) FROM respuestas as res INNER JOIN preguntas as pre
WHERE res.id_pregunta = pre.id_pregunta and res.id_alumno = $id_estudiante AND pre.id_pregunta
IN (select id_pregunta from preguntas where id_perfil=1) group by res.id_alumno";
if ($conexion->connect_errno) {
echo "Fallo al conectar a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$resultado = mysqli_query($conexion, $sql);
while($registro = mysqli_fetch_array($resultado)){
array_push ($array_resultados, $registro[0]);
// //var_dump($registro[0]);
}
$conexion->close();
$datos["estado"] = "exito";
$datos["valores"] = $array_resultados;
$resultadoJson = json_encode($datos);
//var_dump($resultadoJson);
echo $_GET['jsoncallback'] . '(' . $resultadoJson . ');';
exit();
?>
NOTE 1:
The weirdest thing is that when I put the var_dum () to see the content if what it shows is that if the value is 40 that means that everything It's fine but when I take it off it does not bring me anything, it's to say that 40 seems to have disappeared.
NOTE 2:
I also clarify that when I modify my query for this:
"SELECT SUM(respuesta) FROM respuestas";
without taking into account the keys foreign if you show me the result in the console.log (); of javascript.
What I think it could be:
Maybe it will be that the result 40 can not be obtained because when run the PHP query fails because of my relationship structure the foreign keys of the table foreign key answers that contains is: question_id and student_id.