Page 1 sur 1

Print table PHP MySQL

Posté : 01 nov. 2022, 22:03
par uhjbizef
Hello tout le monde,

J'ai un soucis, je veux afficher un tableau avec mes enregistrements de ma bdd MySQL.

Code : Tout sélectionner

<html> <?php include('../dropnft/config/connectiondb.php'); ?> <head> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="/dropnft/images/logotest1.png"> <link href="../dropnft/style.css" rel="stylesheet" type="text/css"> </head> <body> <?php include('../dropnft/config/verticalnavbar.php'); ?> <div style="padding-left: 15vw; padding-top: 10vh; height: 100vh; width: 100%"> <div style="float: left; width:100%;"> <h4 style="text-align: center">Commandes</h4> <table class="table" style="background-color: #ffff;"> <thead> <tr> <th scope="col">#</th> <th scope="col">Date commande</th> </tr> </thead> <?php $query = mysqli_query($connection, "select * from commande"); $row = mysqli_fetch_array($query); ?> <tbody> <?php foreach($row as $q) { echo "<tr> <th scope='row'>".$q['id_commande']."</th> <td>".$q['date_commande']."</td> </tr>"; } ?> </tbody> </table> </div> </div> </body> <?php include('../dropnft/config/footer.php'); ?> </html>
Je vous mets l'erreur rencontrée:

( ! ) Warning: Illegal string offset 'id_commande' in C:\wamp64\www\dropnft\facture.php on line 44
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0

( ! ) Warning: Illegal string offset 'date_commande' in C:\wamp64\www\dropnft\facture.php on line 45
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0
1 1
( ! ) Warning: Illegal string offset 'id_commande' in C:\wamp64\www\dropnft\facture.php on line 44
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0

( ! ) Warning: Illegal string offset 'date_commande' in C:\wamp64\www\dropnft\facture.php on line 45
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0
1 1
( ! ) Warning: Illegal string offset 'id_commande' in C:\wamp64\www\dropnft\facture.php on line 44
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0

( ! ) Warning: Illegal string offset 'date_commande' in C:\wamp64\www\dropnft\facture.php on line 45
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0
2 2
( ! ) Warning: Illegal string offset 'id_commande' in C:\wamp64\www\dropnft\facture.php on line 44
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0

( ! ) Warning: Illegal string offset 'date_commande' in C:\wamp64\www\dropnft\facture.php on line 45
Call Stack
# Time Memory Function Location
1 0.0004 364688 {main}( ) ...\facture.php:0

Merci beaucoup

Re: Print table PHP MySQL

Posté : 01 nov. 2022, 23:16
par or 1
la manière de parcourir les résultats du select n'est pas bonne.

$row = mysqli_fetch_array($query);
ne retourne qu'une seule ligne. pour avoir toutes les lignes et faire un traitement sur toutes les lignes, il faut une boucle.
avec une telle ligne, $row['id_commande'] et $row['date_commande'] fonctionne.

le plus simple est de trouver un tutorial pour trouver une façon correcte de parcourir le résultat d'une requête.