Problem with fetch_all (), does not get rows

1

I'm doing an exercise with fetch_all() , it's supposed to show the data of the BD in an array but it does not show anything.

<?php

try {
    require_once ('includes/funciones/bd_conexion.php');
    $sql = "SELECT * FROM 'registros'; ";
    $resultado = $conn->query($sql);
} catch (Exception $e) {
    $error = $e->getMessage();
}

?>

<?php while($registros= $resultado-> fetch_all() ) { ?>
    <pre>
    <?php var_dump($registros);?>
    </pre>
<?php } ?>

If I change fetch_all() by fetch_assoc() , if it shows the data but in independent arrays. I'm using MAMP .

    
asked by Roger Day MexPsy 19.02.2017 в 20:11
source

3 answers

0

The next time you use the mysqli or PDO libraries to work with your databases, since mysql is currently not used much, but when you are working on accessing a specific field of the table and putting it into a div , or something dynamically, I do it with PDO, and I leave you my way of doing it

              try
               {
        $bbdd_conection = new PDO("mysql:host=localhost;dbname=mi_tabla", "root", "");
                  if
                    ($bbdd_conection->errorCode())
                   {
                    die("Fallo al conectar con la base de datos");
                   }else {
     $bbdd_conection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
               $bbdd_conection->exec('SET CHARACTER SET UTF8');
               $stm = "SELECT * nombre_tabla"
               $objeto = $bbdd_conection->prepare($stm);
               $objeto->execute();
               $resultado_demi_tabla = $objeto->fetchAll(PDO::FETCH_OBJ);
               foreach($resultado_demi_tabla as $valor){
                   print_r($valor) // usarlo para testear
                   echo $valor->campo1

                   //Con esto podras acceder a cada campo que devuelve el registro de tu tabla, si tu tabla se llama nombre,edad etc con $valor->edad podras ver los resultados y utilizarlos.
               }

               //
                   }
               } catch (PDOException $ex) {
                    echo "Fallo al conectar o enviar una consulta a la base de datos en la linea " . $ex->getLine()
                    . " y el causante del fallo es " . $ex->getMessage() . PHP_EOL;
               }finally{
                   $bbdd_conection = NULL;
               }

            }


        }

>

    
answered by 19.02.2017 в 20:45
0

(Assuming you're using mysqli) Unlike the fetch method, the output of fetch_all is not traversed, but used directly.

<?php

try {
    require_once ('includes/funciones/bd_conexion.php');
    $sql = "SELECT * FROM 'registros'; ";
    $resultado = $conn->query($sql);

    echo "<pre>";
    var_dump($resultado->fetch_all(MYSQLI_ASSOC));
    echo "</pre>";
} catch (Exception $e) {
    $error = $e->getMessage();
}

Beware, the methods are called different in mysqli than in PDO . You did not tell us which one you are using.

    
answered by 20.02.2017 в 13:31
0

I found the solution to the problem, change this code:

<?php while($registros= $resultado->fetch_all(MYSQLI_ASSOC)) { ?>
   #codigo 
<?php } ?>

For this other:

<?php $registros= array() ?>
      <?php while($registro= $resultado->fetch_assoc()) { ?>
            <?php $registros[] = $registro; ?>
      <?php } ?>

In this way I obtained the same result. Clarification: if I am using msqli and also use the fecth_all () directly but it does not work. Thanks everyone for your help. Greetings.

    
answered by 20.02.2017 в 18:57