Page 1 sur 2
Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:09
par iso
Bonjour j'essaie de générer un tableau qui s'agrandi en fonction des information dans la bdd voici mon code pour la page :
<?php
$pagetitle = "AFI stock";
$pagelocation = "../";
include("../asset/include/head.php");
{
$reponse= $bdd->query('SELECT `ref`, `stock` FROM `fournisseur` ');
$donnees = $reponse->fetch();
}
?>
<div style ="background-color: #F7F7F7; width: 100%;box-shadow: 11px 12px 20px -7px rgba(0,0,0,0.21);margin-top: 2em;margin-bottom: 2em; border-radius: 10px" class="p-2";>
<form class="form-inline m-0 p-0">
<div class="form-group mx-sm-3" >
<label for="inputPassword2" class="sr-only">Rechercher</label>
<input type="text" class="form-control" id="inputPassword2" placeholder="Rechercher">
</div>
<button type="submit" class="btn btn-primary mr-3"><i class="fas fa-search"></i> Rechercher</button>
</form>
</div>
<table class="table table-striped text-primary" style="text-align: center; margin-top: 3em;">
<thead>
<tr >
<th style="width: 25%;" scope="col">Référence</th>
<th style="width: 25%;" scope="col">En stock ?</th>
<th style="width: 25%;" scope="col">Date</th>
<th style="width: 25%;" scope="col">Modifié par</th>
</tr>
</thead>
<tbody>
<?php var_dump($donnees);
while($donnees=$reponse->fetch());
{
echo '<tr>
<td> '.$donnees[`ref`].'</td>
<td> '.$donnees[`stock`].'</td>
<td></td>
<td></td>
</tr>
</table>';
$reponse->closeCursor();
}
?>
<?php include("../asset/include/footer.php");?>
mais une erreur s'affiche lors du chargement de la page: Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 35
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 36
j'ai vérifié si la variable était vide mais le var_dump m'affiche bien les données contenue dans la table de la bdd: array(4) { ["ref"]=> string(8) "JZH-TC-2" [0]=> string(8) "JZH-TC-2" ["stock"]=> string(3) "oui" [1]=> string(3) "oui" }
Avez vous des idée ?
merci.
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:21
par Shadowwera
Salut, cela vient probablement du fait que tu fetch 2 fois tes données. Essaye en enlevant le " $donnees = $reponse->fetch(); " en dessous de ton query
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:24
par iso
Merci de t'as réponse je viens d'essayer et si je fais ça la variable donnees n'as plus de valeur et j'ai toujours comme erreur:
Warning: Undefined variable $donnees in C:\xampp\htdocs\CHR\Stock\afi.php on line 31
NULL
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 35
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 36
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:40
par Shadowwera
C'est normal qu'elle n'ai pas de valeur puisque tu fait ton var_dump($donnees) avant ton while.
Pour l'erreur :
un poste similaire
EDIT : Ton "$reponse->closeCursor();" devrais être en dehors de ta boucle, idem pour ton </table> car cela va fermer le tableau et clôturer la requête dès la 1ère occurrence
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:46
par iso
Ah oui en effet, cependant malgré le fait que j'ai retiré le fetch() en dessous du query l'erreur et toujours la.
J'ai déjà regardé ce post mais je n'ai pas oublié le closeCursor() donc il ne m'as pas beaucoup aidé.
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 11:50
par Shadowwera
Je viens de tester de mon coté, ce code :
<?php
$stmt = $dbh->query("SELECT id, ID_TECH FROM BDC_COMMANDE");
while($donnee = $stmt->fetch()) {
echo $donnee['id']."<br/>";
echo $donnee['ID_TECH']."<br/>";
}
$stmt->closeCursor();
?>
Fonctionne parfaitement. Si chez toi ça ne fonctionne pas, c'est que ça doit venir de ta connexion à ta base de données
EDIT : OK je viens de voir ton erreur, remplace tes ` par des guillemets simple ' ou double " la où tu as ecrit $donnee[`ref`] et $donnee[`stock`]
`est exclusivement réservé à indiquer une table/colonne dans une requête SQL
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 12:02
par iso
L'erreur doit venir de la connexion à la base de données car même en changeant par des guillemets l'erreur s'affiche toujours.
je vais enquêter de mon côté merci pour t'es réponse !
EDIT j'ai quand même un doute sur la connexion sur la base de donnée car ma variable contient bien les infos qui sont contenu dedans
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 12:15
par or 1
on peut avoir la dernière version du code ?
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 12:30
par iso
Bien sûr,
<?php
$pagetitle = "AFI stock";
$pagelocation = "../";
include("../asset/include/head.php");
{
$reponse= $bdd->query('SELECT `ref`, `stock` FROM `fournisseur` ');
}
?>
<div style ="background-color: #F7F7F7; width: 100%;box-shadow: 11px 12px 20px -7px rgba(0,0,0,0.21);margin-top: 2em;margin-bottom: 2em; border-radius: 10px" class="p-2";>
<form class="form-inline m-0 p-0">
<div class="form-group mx-sm-3" >
<label for="inputPassword2" class="sr-only">Rechercher</label>
<input type="text" class="form-control" id="inputPassword2" placeholder="Rechercher">
</div>
<button type="submit" class="btn btn-primary mr-3"><i class="fas fa-search"></i> Rechercher</button>
</form>
</div>
<table class="table table-striped text-primary" style="text-align: center; margin-top: 3em;">
<thead>
<tr >
<th style="width: 25%;" scope="col">Référence</th>
<th style="width: 25%;" scope="col">En stock ?</th>
<th style="width: 25%;" scope="col">Comment ?</th>
<th style="width: 25%;" scope="col">Modifié par</th>
</tr>
</thead>
<tbody>
<?php
while($donnees=$reponse->fetch());
{
echo '<tr>
<td> '.$donnees['ref'].'</td>
<td> '.$donnees['stock'].'</td>
<td></td>
<td></td>
</tr>
</table>';
}
$reponse->closeCursor();
?>
<?php include("../asset/include/footer.php");?>
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 12:43
par or 1
et l'erreur générée par ce code ?
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 12:49
par iso
Toujours la même :
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 35
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CHR\Stock\afi.php on line 36
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 13:00
par or 1
en enlevant les { et } de
{
$reponse= $bdd->query('SELECT `ref`, `stock` FROM `fournisseur` ');
}
?????
$reponse->fetch() retourne false car il y a une erreur. le var_dump montre que la requête a bien été exécutée, donc c'est assez incompréhensible.
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 13:47
par iso
je viens d'essayer ça m'affiche toujours la même erreur;
Oui je suis d'accord je ne comprend pas bien d'ou ça peut venir.
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 14:02
par Shadowwera
Peut tu nous montrer comment tu fait ta connexion à ta bdd ?
Re: Erreur Trying to access array offset on value of type bool
Posté : 02 juin 2021, 14:10
par iso
Dans un head.php qui est en include en haut du premier code:
<?php
if ($pagetitle == "")
{
$pagetitle = "dashboard";
}
if ($pagelocation == "")
{
$pagelocation = "";
}
try
{
$bdd= new PDO('mysql:host=localhost;dbname=chrstock','root','');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
?>
<head>
<meta charset="UTF-8">
<title><?= $pagetitle ?></title>
<link href="<?= $pagelocation ?>asset/css/style.css" rel="stylesheet">
<link href="<?= $pagelocation ?>asset/css/styleperso.css" rel="stylesheet">
<link href="<?= $pagelocation ?>asset/css/menu.css" rel="stylesheet">
<!--fontawesome-->
<script src="https://kit.fontawesome.com/2bdd874a67.js" crossorigin="anonymous"></script>
</head>
<body class="bg-light text-light">
<div class="wrapper">
<!-- Sidebar Holder -->
<nav class="bg-primary" id="sidebar">
<div class="sidebar-header text-center">
<h3>Menu</h3>
</div>
<ul class="list-unstyled components bg-primary">
<li class="active">
<a href="<?= $pagelocation ?>dashboard.php"><i class="fas fa-chart-line"></i> Dashboard</a>
<a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><i class="fas fa-cubes"></i></i> Stock</a>
<ul class="collapse list-unstyled bg-primary" id="homeSubmenu">
<li>
<a href="<?= $pagelocation ?>Stock/Diamond.php">Diamond stock</a>
</li>
<li>
<a href="<?= $pagelocation ?>Stock/Combisteel.php">Combisteel stock</a>
</li>
<li>
<a href="<?= $pagelocation ?>Stock/Virtus.php">Virtus stock</a>
</li>
<li>
<a href="<?= $pagelocation ?>Stock/afi.php">AFI Stock</a>
</li>
</ul>
</li>
<li>
<a href="#homeSubmenu2" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><i class="far fa-arrow-alt-circle-down"></i></i> Réaprovisionnement</a>
<ul class="collapse list-unstyled bg-primary" id="homeSubmenu2">
<li>
<a href="<?= $pagelocation ?>Réapro/Diamond.php">Diamond réapro</a>
</li>
<li>
<a href="<?= $pagelocation ?>Réapro/Combisteel.php">Combisteel réapro</a>
</li>
<li>
<a href="<?= $pagelocation ?>Réapro/Virtus.php">Virtus réapro</a>
</li>
<li>
<a href="<?= $pagelocation ?>Réapro/afi.php">AFI réapro</a>
</li>
</ul>
</li>
<li>
<a href="<?= $pagelocation ?>Nouvelle saisie.php"><i class="fas fa-plus-circle"></i> Ajout stock ou réapro</a>
</ul>
</nav>
<!-- Page Content Holder -->
<div id="content">
<nav class="navbar navbar-expand-lg navbar-light bg-primary" style="border-radius: 10px">
<div class="d-flex flex-row w-100">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<div class="d-flex flex-row justify-content-between w-100">
<div class="m-0">
<button type="button" id="sidebarCollapse" class="navbar-btn bg-primary" style="margin-right: 5em;">
<span></span>
<span></span>
<span></span>
</button>
</div>
<div>
<h2 class="m-0"> <?=$pagetitle ?></h2>
</div>
<div class=" d-flex flex-row" >
<h3 class="mr-2">Utilisateur</h3>
<p class="m-0 p-0"><button type="button" class="btn btn-outline-light" style="vertical-align: baseline";><i class="fas fa-sign-out-alt"></i> Déconnexion</button></p>
</div>
</div>
</div>
</div>
</nav>