Boucle Infini

Par Ekim Kael, il y a 9 ans


Bonjour,
Je voudrais recuperer toute les entrées en BDD.
il n'y en a que deux mais j'ai une boucle infini sur le premier élément

<?php require 'inc/header.php'; require 'inc/db.php'; $q = $db->query('SELECT * FROM users'); $users = $q->fetch(PDO::FETCH_OBJ); ?> <div class="row"> <div class="col-xs-12 col-sm-9 col-md-9 col-lg-9"> <div class="col-lg-offset-5"> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Utilisateurs</a></li> <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">pieces perdus</a></li> </ul> </div> <!-- Tab panes --> <div class="tab-content"> <div role="tabpanel" class="tab-pane active" id="home"> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th>Nom</th> <th>Prenom</th> <th>Email</th> <th>Telephone</th> <th>CNI</th> </tr> </thead> <tbody> <?php while ($users) { ?> <tr> <th><?= $users->name; ?></th> <th><?= $users->username; ?></th> <th><?= $users->email; ?></th> <th><?= $users->phone; ?></th> <th><?= $users->cni; ?></th> </tr> <?php } ?> </tbody> </table> </div> </div> <div role="tabpanel" class="tab-pane" id="profile"> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th>Type</th> <th>Localisation</th> <th>Nom du proprétaire</th> <th>Telephone</th> </tr> </thead> <tbody> <tr> <th>Passeport</th> <th>Gendarmerie de Ngousso</th> <th>MichKael Kaiser</th> <th>694955064</th> </tr> <tr> <th>Carte d'identité National</th> <th>GSO Elig-Essono</th> <th>John Doe</th> <th>66542386</th> </tr> </tbody> </table> </div> </div> </div> </div> <div class="col-sm-3 col-md-2 col-lg-2"> <ul class="list-group"> <li class="list-group-item">Cras justo odio</li> <li class="list-group-item">Dapibus ac facilisis in</li> <li class="list-group-item">Morbi leo risus</li> <li class="list-group-item">Porta ac consectetur ac</li> <li class="list-group-item">Vestibulum at eros</li> </ul> </div> </div> <?php require 'inc/footer.php'; ?>

7 réponses

pulsat, il y a 9 ans

Je crois que le PDO doit etre comme ceci :

<?php PDO::FETCH_ASSOC ?>
arnich, il y a 9 ans

Salut,

La methode fecth te retourne l'enregistrement suivant de ta query, false une fois qu'il y a plus d'enregistrement a lire.
Donc l'erreur est dans la condition de ton while

while ($users = $q->fetch(PDO::FETCH_OBJ)){ // Ton traitement }
Ekim Kael, il y a 9 ans

Je dois en fait utiliser fetchAll()
Mais là encore problème
Notice: Undefined index: name in C:\wamp\www\losthing\admin\account.php

Sparkosis, il y a 9 ans

Salut, tu peux faire de cette manière

while ($users = $user->fetch()) { $username = $user['name']; }

Si j'ai bien compris c'est ça que tu attends.

Ekim Kael, il y a 9 ans

oui @Sparkosis un truc comme ça

arnich, il y a 9 ans
while ($users = $q->fetch(PDO::FETCH_OBJ)){ $username = $users->name }
Ekim Kael, il y a 9 ans

merci les Gars