Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Arno Welzel Newsgroups: comp.lang.php Subject: Re: Help With Warning Message Date: Mon, 30 Aug 2021 10:21:12 +0200 Lines: 85 Message-ID: References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net nziET9ITSPqNXg7sjKdBEwubvcH+B2YQorgHlNr8Z7IDe9L7OQ Cancel-Lock: sha1:jNT+bQmQ1l72Qq197VXdG1fOgM8= In-Reply-To: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com> Xref: csiph.com comp.lang.php:18783 Call Me Tom: > > here's the code > $query4 = "SELECT airport_name > FROM airports > WHERE airport_code = '$origin'"; > $result4 = $dbh->query($query4); > $da = $result4->FETCH(PDO::FETCH_ASSOC); > $da_name = $da['airport_name']; > > The above produces this warning message > > Warning: Trying to access array offset on value of type bool in > C:\xampp\htdocs\CAA_Tom\logbook.php on line 104 In line 104 of your code you try to access a variable as array but the variable is a bool. I believe this is your problem: $da = $result4->FETCH(PDO::FETCH_ASSOC); $da_name = $da['airport_name']; 1) Do NOT just copy & paste code which you find somewhere! Learn how PHP and PDO works first! 2) The method is not "FETCH()" but "fetch()", also see here: 3) Read the documentation: "The return value of this function on success depends on the fetch type. In all cases, false is returned on failure." This means: if the fetch did not return anything the result will not be an array but the boolean value "false". You should also never use any string in an SQL statement without escaping! This will most likely cause a security issue due to possible SQL injections one day! A good way to avoid SQL injections is to use prepared statements, also see here: So one solution could be: // Prepare the SELECT statement with a parameter for the code $statement = $dbh->prepare( "SELECT airport_name FROM airports WHERE airport_code = :code" ); // Bind the parameter $statement->bindParam(':code', $origin); // Execute the prepared statement $result4 = $statement->execute(); // Fetch the result $da = $result4->fetch(PDO::FETCH_ASSOC); // Only use the result if there is one, otherwise set the airport // name to an empty string // // Also keep in mind that "yoda conditions" (value first, variable // second) are the preferred way to avoid accidental assignments of // values (e.g. "if ($var = true)" instead of "if ($var === true)") if (false !== $da) { $da_name = $da['airport_name']; } else { $da_name = ''; } -- Arno Welzel https://arnowelzel.de