0

I am using the following PHP Script to get a row from a table:

//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");

if ($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
    return;
}




$sql = "SELECT 
tarea.id as id,
tarea.usuario_asignado as usuario_asignado_id,
users.nombre as usuario_asignado_nombre,
users.apellidos as usuario_asignado_apellidos,
users.email as usuario_asignado_email,
tarea.descripcion as descripcion,
tarea.latitud as latitud,
tarea.longitud as longitud,
tarea.ubicacion as ubicacion,
tarea.titulo as titulo,
tarea.edificio as edificio,
tarea.planta as planta,
tarea.estancia as estancia,
tarea.categoria as categoria_id,
categoria.categoria as categoria_categoria,
tarea.subcategoria as subcategoria_id,
subcategoria.subcategoria as subcategoria_subcategoria,
tarea.tipo as tipo_id,
tipo.tipo as tipo_tipo,
tarea.fecha_hora_vencimiento as fecha_hora_vencimiento,
tarea.fecha_hora_inicio_programada as fecha_hora_inicio_programada,
tarea.fecha_hora_inicio_efectiva as fecha_hora_inicio_efectiva,
tarea.fecha_hora_fin as fecha_hora_fin,
tarea.expediente as expediente_id,
exp.titulo as expediente_titulo,
tarea.asignada_a_usuario as asignada_a_usuario,
tarea.asignada_a_grupo as asignada_a_grupo,
tarea.grupo as grupo_id,
grupo.grupo_int as grupo_int,
tarea.estado as estado_id,
estado.estado as estado_estado


FROM tb_tareas tarea

LEFT JOIN tb_usuarios_internos userint ON tarea.usuario_asignado = userint.id_usuario
LEFT JOIN tb_usuarios users ON users.id = userint.id_usuario
LEFT JOIN tb_categorias_tareas categoria ON categoria.id = tarea.categoria
LEFT JOIN tb_subcategoria_tareas subcategoria ON subcategoria.id = tarea.subcategoria
LEFT JOIN tb_tipos_tareas tipo ON tipo.id = tarea.tipo
LEFT JOIN tb_expedientes exp ON exp.id = tarea.expediente
LEFT JOIN tb_grupos_internos grupo ON grupo.id = tarea.grupo
LEFT JOIN tb_estados_tareas estado ON estado.id = tarea.estado
WHERE tarea.usuario_asignado = '182'
ORDER BY estado.estado DESC, tarea.fecha_hora_inicio_programada
 ";
$result = $conn->query($sql);

$response = array();

if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        array_push($response,$row);
    }
    $conn->close;
header('Content-Type: application/json');
echo json_encode($response);
}
else {
    echo "vacio";
}

The result after executing the script is the last echo line, not the result.

Executing the query using PHPMyAdmin throws the expected result, which is a row from the table tb_tareas.

What is wrong in my script?

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • 1
    [num_rows](https://www.php.net/manual/en/mysqli-result.num-rows.php) doesn't always work the way you expect. You might have better luck using [fetch_all()](https://www.php.net/manual/en/mysqli-result.fetch-all.php) instead of using a loop since you're just creating an array anyway, then check to see if that's empty before echoing what you need. – aynber Jul 22 '22 at 13:18
  • The result is ''vacio''? Therefore, result is empty am I right? If you dump and die the result variable what do you get? – Yann Chabot Jul 22 '22 at 13:18
  • Also, I suggest you looking into PDO, it's very stable and object oriented way of doing MySQL Queries. https://www.php.net/manual/pdo.connections.php – Yann Chabot Jul 22 '22 at 13:22
  • either there is an error or your script is connecting to a different database – Your Common Sense Jul 22 '22 at 13:24
  • Just for clarity - obviously, PHP doesn't query your database any other way than phpmyadmin does. Being connected to the same database, both will return the same data, given the query is the same. – Your Common Sense Jul 22 '22 at 13:26
  • @YourCommonSense, that´s obvious. But if the query is throwing the result, then the issue is in the PHP script, that is the point of my question – mvasco Jul 22 '22 at 13:28
  • EXACTLY the opposite: the problem is anywhere but in the PHP script, given the code is correct and it throws no errors. – Your Common Sense Jul 22 '22 at 13:32

0 Answers0