Statements prepared in MSQLI

0

Edited I add the ajax where I make the request

$.ajax({
        //tipo de peticion puede ser
        type: "POST",
        //url del php
        url: "pcService.php",
        //separado por comas
        data: {
            id: idPC,
            comando: 1,
        },
        //tipo de dato a recibir
        dataType: "json",
        //en el caso de algun error que mande un mensaje
        error: function (jqXHR, textStatus, errorThrown) {
            swal(
                'Error!',
                errorThrown,
                'error'
            );
        },
        //si hay conexion obtenemos la data que nos envia el php
        success: function (data, textStatus, jqXHR) {
           console.log(data);
        }

    });

I have the following function.

$comando = $_POST["comando"];
switch ($comando){
 case 1:
    //get Asignaciones
     $idPc = $_POST["id"];
    echo asignacionPcLista($idPc);
    break;

}

function asignacionPcLista($idPc){
 GLOBAL $bd;
 try {
     $arrayInfo = array();
     $conexion = $bd->getConecction();
     $query = "SELECT sp.nombre,ac.fecha FROM soporte_compu AS c 
                    INNER JOIN soporte_asignacion_compu AS ac ON ac.SOPORTE_COMPU_idSOPORTE_COMPU = c.idSOPORTE_COMPU 
                    INNER JOIN soporte_personal AS sp ON sp.idSOPORTE_PERSONAL = ac.SOPORTE_PERSONAL_idSOPORTE_PERSONAL 
                    WHERE c.idSOPORTE_COMPU = ? AND ac.status = ?";
     $stmt = $conexion->prepare($query);
     $stmt->bind_param("is", $idPc,$estatus = '1');
     $stmt->execute();
     $resultSet = $stmt->get_result();
     $stmt->close();
     array_push($arrayInfo,["asignaciones"=>$resultSet->fetch_array(MYSQLI_ASSOC)]);
     //array_push($arrayInfo,["informacion"=>getPC($idPc)]);
     return json_encode($arrayInfo, true);
 } catch (mysqli_sql_exception $e) {
     return json_encode(["error" => "Error al modificar la asignación, " . $e->getMessage()], true);
 }
}

This sentence gets me all the users that have a pc assigned. When it comes to a user if I throw the data well, the detail is in that when more than one for example: 5 users do not throw anything at me.

Here I show the result that should be waiting, but when I want to get it in php does not throw anything at me, only throws when there is a user in that assignment but when many are assigned does not show me anything.

    
asked by Miguel Osorio 14.02.2017 в 19:28
source

1 answer

0

What happens is that the function fetch_array() only returns the first row of the results, you should do a dump with something like a while:

$stmt->execute();  
$resultSet = $stmt->get_result();  
$stmt->close();  
$x = 0;  
$arrayFinal = Array();  
while($temporal = $resultSet->fetch_assoc()) {  
    $arrayFinal[$x] = $temporal;  
    $x ++;  
}  
array_push($arrayInfo,["asignaciones"=>$arrayFinal]);  
//array_push($arrayInfo,["informacion"=>getPC($idPc)]);  

return json_encode($arrayInfo, true);

I also recommend that when you want to know if an array is being passed or obtained correctly, use print_r() in your development environment to visualize the structure of your array, and thus understand a little better what could be happening when overturning. the data. For example:

print_r($arrayInfo);
    
answered by 14.02.2017 в 20:59