0

Tengo el siguiente código php pero sólo me devuelve una fila, no sé por qué será la tabla tiene 70 filas y la consulta pide entre dos números, estos números vienen de formulario.

Si pongo 1 y 60 o 13 y 40 o algo que se espera devuelva más de una fila siempre regresa sólo la primera fila en el array $tables

  require '../conn.php';

  $from = $_POST['from'];
  $to = $_POST['to'];

  $sql = "SELECT * FROM senders WHERE id BETWEEN {$from} AND {$to}"; 
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    $response->sql = $sql;
    $tables = array();
    while($row = mysqli_fetch_assoc($result)) { 

      $table = new stdClass;

      $table->id = $row['id'];
      $table->name = $row['name'];
      $table->work = $row['work'];
      $table->domain = $row['domain'];
      $table->email = $row['email'];
      $table->emailEn = $row['emailEn'];
      $table->date = $row['date'];

      $table->qListDB = implode(" * ",json_decode($row['qListDB']));
      $table->area = $row['area'];
      $table->level = $row['level'];
      $table->phone = $row['phone'];
      $table->nameEn = $row['nameEn'];


      $sql = "SELECT * FROM aplications WHERE ide = '{$row['ide']}'"; 
      $result = mysqli_query($conn, $sql);
      if (mysqli_num_rows($result) > 0) {
        $questSolved = [];
        while($row = mysqli_fetch_assoc($result)) {
          array_push($questSolved,$row['idUrl']);
          $table->age = $row['age'];
          $table->school = $row['school'];
        }
        $table->questSolved = $questSolved;

      } else {
        $table->age = " - ";
        $table->school = " - ";
        $table->questSolved = " - ";
      }
      array_push($tables,$table);
    }
    $response->state = true;
    $response->tables = $tables;
    echo json_encode($response);
  } else {
    $response->state = false;
    echo json_encode($response);
  }

1 respuesta 1

0

Lo solucioné el problema eran los nombres de las variables, estaba usando los mismo nombres en las dos consultas que tenía

<?php 

  require '../conn.php';

  $from = $_POST['from'];
  $to = $_POST['to'];

  $sql = "SELECT * FROM senders WHERE id BETWEEN {$from} AND {$to}"; 
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    $response->sql = $sql;
    $response->num = mysqli_num_rows($result);
    $tables = array();
    while($row = mysqli_fetch_assoc($result)) { 

      $table = new stdClass;

      $table->id = $row['id'];
      $table->name = $row['name'];
      $table->work = $row['work'];
      $table->domain = $row['domain'];
      $table->email = $row['email'];
      $table->emailEn = $row['emailEn'];
      $table->date = $row['date'];

      $table->qListDB = implode(" * ",json_decode($row['qListDB']));
      $table->area = $row['area'];
      $table->level = $row['level'];
      $table->phone = $row['phone'];
      $table->nameEn = $row['nameEn'];


      $sql2 = "SELECT * FROM aplications WHERE ide = '{$row['ide']}'"; 
      $result2 = mysqli_query($conn, $sql2);
      if (mysqli_num_rows($result2) > 0) {
        $questSolved = [];
        while($row2 = mysqli_fetch_assoc($result2)) {
          array_push($questSolved,$row2['idUrl']);
          $table->age = $row2['age'];
          $table->school = $row2['school'];
        }
        $table->questSolved = $questSolved;
        array_push($tables,$table);

      } else {
        $table->age = " - ";
        $table->school = " - ";
        $table->questSolved = " - ";
      array_push($tables,$table);

      }
    }
    $response->state = true;
    $response->tables = $tables;
    echo json_encode($response);
  } else {
    $response->state = false;
    echo json_encode($response);
  }

?>
2
  • 1
    podrias optimizar un poco la consulta, haciendo un join entre las tablas applications y senders. Con eso solo tendrias que leer cada linea de resultados, en lugar de hacer varias consultas, como haces en tu codigo
    – Jakala
    el 14 oct. 2018 a las 22:32
  • 1
    Y podrías usar consultas preparadas ya que actualmente tu código es altamente vulnerable a ataques de inyección SQL.
    – A. Cedano
    el 15 oct. 2018 a las 9:42

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.