Why does a while loop show no existing data when used in PHP 5.3.0?

1

I have a while cycle to show a list of data using the following code:

 <?php
    $stmt = $conexion->prepare("SELECT desc_unix ,cos_unix FROM unidadesx");
    $stmt->execute();
    $resultados = $stmt->get_result();
    $contador = 0;

    while ($datos = $resultados->fetch_assoc())
      {
      $contador = $contador + 1;
      echo '<td bordercolor="#FFFFFF" align="center" width="112">' . $contador . '</td>';
      echo '<td bordercolor="#FFFFFF" align="center" width="192">' . $datos["cos_unix"] . '</td>';
      echo '<td bordercolor="#FFFFFF" align="center" width="596">' . $datos["desc_unix"] . '</td>';
      echo "</tr>";
      }

    $stmt->close();
    ?>

This works well in PHP 5.4.0. When passed to PHP 5.3.0 it shows nothing.

  • The first thing I did was to test the value of $resultados , since up to where this sentence was the HTML was displayed, I had to change from get_result(); to store_result(); to show the whole document.

  • The second thing was that it generated the columns but the data did not think the problem was in fetch_assoc() and change it by mysql_fetch_array .

  • Now it shows all the HTML content but not the data, that is, it leaves column 1, 2, 3 but empty.

Here is the final code:

<?php
$stmt = $conexion->prepare("SELECT desc_unix ,cos_unix FROM unidadesx");
$stmt->execute();
$resultados = $stmt->store_result();
$contador = 0;

while ($datos = mysqli_fetch_array($resultados,MYSQL_ASSOC))
  {
  $contador = $contador + 1;
  echo '<td bordercolor="#FFFFFF" align="center" width="112">' . $contador . '</td>';
  echo '<td bordercolor="#FFFFFF" align="center" width="192">' . $datos["cos_unix"]. '</td>';
  echo '<td bordercolor="#FFFFFF" align="center" width="596">' . $datos["desc_unix"]. '</td>';
  echo "</tr>";
  }

$stmt->close();
?>

How can I make this cycle work in PHP 5.3?

    
asked by Victor Alvarado 02.05.2017 в 14:50
source

1 answer

3

Why not use as in the documentation states:

$sql = "SELECT desc_unix ,cos_unix FROM unidadesx";
$contador = 0;
foreach ($conexion->query($sql) as $row) {
    $contador = $contador + 1;
    echo '<td bordercolor="#FFFFFF" align="center" width="112">' . $contador . '</td>';
    echo '<td bordercolor="#FFFFFF" align="center" width="192">' . $row["cos_unix"]. '</td>';
    echo '<td bordercolor="#FFFFFF" align="center" width="596">' . $row["desc_unix"]. '</td>';
    echo "</tr>";
}
    
answered by 02.05.2017 в 15:03