[RESOLU] Erreur Trying to access array offset on value of type bool

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : [RESOLU] Erreur Trying to access array offset on value of type bool

Re: Erreur Trying to access array offset on value of type bool

par iso » 03 juin 2021, 11:25

c'était ce ';' qui me causais tant de soucis ! merci beaucoup pour ton aide

Re: Erreur Trying to access array offset on value of type bool

par Shadowwera » 03 juin 2021, 10:53

Donc ta connexion à ta BDD est bonne ...

Pourquoi mettre un ";" après ton while ?

Tu as d'ailleurs récupéré toutes les infos, donc le while n'est pas utile dans ce cas : Essaye de passer par un foreach
foreach($donnees as $value) : ?>
	<tr>
		<td><?= $value['ref'] ?></td>
		<td><?= $value['stock'] ?></td>
		<td></td>
		<td></td>
	</tr>
<?php
endforeach;
echo '</table>';
EDIT : Pense bien à remettre $donnees = $reponse->fetch(); après ta query

Re: Erreur Trying to access array offset on value of type bool

par iso » 03 juin 2021, 09:01

Désolé je n'avais pas vu t'as réponse et rien ne semble fonctionner le var_dump n'affiche rien

EDIT: j'ai pas assez dormi j'ai modifié le mauvais fichier ...
le var_dump m'affiche:

array(37) { [0]=> array(2) { ["ref"]=> string(8) "JZH-TC-2" ["stock"]=> string(3) "oui" } [1]=> array(2) { ["ref"]=> string(8) "JUS-RQ-4" ["stock"]=> string(3) "oui" } [2]=> array(2) { ["ref"]=> string(5) "AAF6P" ["stock"]=> string(3) "non" } [3]=> array(2) { ["ref"]=> string(5) "AAF6N" ["stock"]=> string(3) "non" } [4]=> array(2) { ["ref"]=> string(6) "AAF12P" ["stock"]=> string(3) "non" } [5]=> array(2) { ["ref"]=> string(6) "AAF12N" ["stock"]=> string(3) "non" } [6]=> array(2) { ["ref"]=> string(5) "AAF7P" ["stock"]=> string(3) "non" } [7]=> array(2) { ["ref"]=> string(5) "AAF7N" ["stock"]=> string(3) "non" } [8]=> array(2) { ["ref"]=> string(6) "AAF14P" ["stock"]=> string(3) "non" } [9]=> array(2) { ["ref"]=> string(6) "AAF14N" ["stock"]=> string(3) "non" } [10]=> array(2) { ["ref"]=> string(5) "AA7PP" ["stock"]=> string(3) "oui" } [11]=> array(2) { ["ref"]=> string(6) "AAS901" ["stock"]=> string(3) "oui" } [12]=> array(2) { ["ref"]=> string(7) "AAS903T" ["stock"]=> string(3) "non" } [13]=> array(2) { ["ref"]=> string(5) "AA7PN" ["stock"]=> string(3) "oui" } [14]=> array(2) { ["ref"]=> string(7) "AAS900V" ["stock"]=> string(3) "oui" } [15]=> array(2) { ["ref"]=> string(7) "AAS903V" ["stock"]=> string(3) "non" } [16]=> array(2) { ["ref"]=> string(7) "AAPS900" ["stock"]=> string(3) "non" } [17]=> array(2) { ["ref"]=> string(7) "AAPS903" ["stock"]=> string(3) "non" } [18]=> array(2) { ["ref"]=> string(6) "AAS2PP" ["stock"]=> string(3) "oui" } [19]=> array(2) { ["ref"]=> string(6) "AAS3PP" ["stock"]=> string(3) "non" } [20]=> array(2) { ["ref"]=> string(6) "AAS2PD" ["stock"]=> string(3) "non" } [21]=> array(2) { ["ref"]=> string(6) "AAS3PD" ["stock"]=> string(3) "non" } [22]=> array(2) { ["ref"]=> string(5) "AA2PP" ["stock"]=> string(3) "non" } [23]=> array(2) { ["ref"]=> string(5) "AA3PP" ["stock"]=> string(3) "non" } [24]=> array(2) { ["ref"]=> string(5) "AA4PP" ["stock"]=> string(3) "non" } [25]=> array(2) { ["ref"]=> string(5) "AA2PD" ["stock"]=> string(3) "non" } [26]=> array(2) { ["ref"]=> string(5) "AA3PD" ["stock"]=> string(3) "non" } [27]=> array(2) { ["ref"]=> string(5) "AA4PD" ["stock"]=> string(3) "non" } [28]=> array(2) { ["ref"]=> string(5) "AA2PZ" ["stock"]=> string(3) "non" } [29]=> array(2) { ["ref"]=> string(5) "AA3PZ" ["stock"]=> string(3) "non" } [30]=> array(2) { ["ref"]=> string(6) "AA28PP" ["stock"]=> string(3) "oui" } [31]=> array(2) { ["ref"]=> string(6) "AA38PP" ["stock"]=> string(3) "non" } [32]=> array(2) { ["ref"]=> string(9) "AA28PPDM2" ["stock"]=> string(3) "oui" } [33]=> array(2) { ["ref"]=> string(9) "AA38PPDM3" ["stock"]=> string(3) "non" } [34]=> array(2) { ["ref"]=> NULL ["stock"]=> NULL } [35]=> array(2) { ["ref"]=> NULL ["stock"]=> NULL } [36]=> array(2) { ["ref"]=> NULL ["stock"]=> NULL } }

Re: Erreur Trying to access array offset on value of type bool

par Shadowwera » 02 juin 2021, 14:18

Essaye ceci à la place de ta connexion :
try {
			$dbh = new PDO("mysql:host=localhost;dbname=chrstock", "root", "");
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch(PDOException $e) {
			die('Connexion failed : '.$e->getMessage());
		}
Pour l'affichage des erreurs

EDIT : Si juste apres ton query tu met ceci :
$donnees=$reponse->fetchAll(PDO::FETCH_ASSOC);
var_dump($donnees);
Que t'affiche t'il ?

Re: Erreur Trying to access array offset on value of type bool

par iso » 02 juin 2021, 14:10

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>

Re: Erreur Trying to access array offset on value of type bool

par Shadowwera » 02 juin 2021, 14:02

Peut tu nous montrer comment tu fait ta connexion à ta bdd ?

Re: Erreur Trying to access array offset on value of type bool

par iso » 02 juin 2021, 13:47

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

par or 1 » 02 juin 2021, 13:00

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

par iso » 02 juin 2021, 12:49

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

par or 1 » 02 juin 2021, 12:43

et l'erreur générée par ce code ?

Re: Erreur Trying to access array offset on value of type bool

par iso » 02 juin 2021, 12:30

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

par or 1 » 02 juin 2021, 12:15

on peut avoir la dernière version du code ?

Re: Erreur Trying to access array offset on value of type bool

par iso » 02 juin 2021, 12:02

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

par Shadowwera » 02 juin 2021, 11:50

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

par iso » 02 juin 2021, 11:46

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é.