Go through an array of a query in PHP

1

I have the following function in PHP:

function ListarDependencias() {

  $db = new Conexion();
  $sql = $db->query("SELECT Id, Nombre FROM Dependencias;") or die (mysqli_error($db));

  if($db->mysqli_num_rows($sql) > 0) {
    $result = $db->mysqli_fetch_array($sql);
  } else {
    $result = null;
  }
  $db->liberar($sql);
  $db->close();

  return $result;
}

Which I want to use to fill the next select with the following code:

<select id="selDependencia" name="selDependencia" class="form-control" required >
   <option value="0"><--Seleccione--></option>
   <?php
     foreach (ListarDependencias() as $dataDependencias) {
       echo "<option value='".$dataDependencias['Id']."'>".$dataDependencias['Nombre']."</option>";
     }
   ?>

I have only two records in my table, however it shows me in select 4 and only its first characters as shown in the following image:

I do not want to have to bring the database scripts to my presentation where the select is, I would like to be able to go through the array and show the data. So I appreciate any help you could give me. Maybe I'm calling wrong the values of my array that my query returns.

    
asked by Alcibiades Palestini 09.10.2018 в 19:24
source

1 answer

1

I made the change you indicated specifying the type of result and with that I could solve. Thank you very much.

$result = $db->mysqli_fetch_all($sql, MYSQLI_ASSOC);
    
answered by 09.10.2018 / 19:47
source