Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

People who go to conferences are the ones who shouldn't.


devel / comp.lang.php / Re: Help With Warning Message

SubjectAuthor
* Help With Warning MessageCall Me Tom
+* Re: Help With Warning MessageLew Pitcher
|`* Re: Help With Warning MessageCall Me Tom
| +* Re: Help With Warning MessageJ.O. Aho
| |`* Re: Help With Warning MessageLew Pitcher
| | +- Re: Help With Warning MessageCall Me Tom
| | `- Re: Help With Warning MessageJ.O. Aho
| `- Re: Help With Warning MessageLew Pitcher
`- Re: Help With Warning MessageArno Welzel

1
Help With Warning Message

<7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=340&group=comp.lang.php#340

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.iad.POSTED!not-for-mail
From: noemail@noemail.com (Call Me Tom)
Newsgroups: comp.lang.php
Subject: Help With Warning Message
Message-ID: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 18
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Mon, 23 Aug 2021 00:01:32 UTC
Date: Sun, 22 Aug 2021 20:00:04 -0400
X-Received-Bytes: 1085
 by: Call Me Tom - Mon, 23 Aug 2021 00:00 UTC

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

I don't even know where to begin. What is the warning trying to tell
me?

Tom

Re: Help With Warning Message

<sfuut5$lb8$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=341&group=comp.lang.php#341

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Date: Mon, 23 Aug 2021 01:50:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sfuut5$lb8$1@dont-email.me>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 23 Aug 2021 01:50:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="feff7aca22ce181f8c076f8cf1e2a6ee";
logging-data="21864"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fqiMV6Ibowo9CJhh0po+PVhfrib1LljY="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:7OU6ylS7w1NaGWYZEeTRAwSkahw=
 by: Lew Pitcher - Mon, 23 Aug 2021 01:50 UTC

On Sun, 22 Aug 2021 20:00:04 -0400, Call Me Tom wrote:

> 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
>
> I don't even know where to begin.

Start with looking at line 104 of the PHP code in file
C:\xampp\htdocs\CAA_Tom\logbook.php

> What is the warning trying to tell me?

On line 104, you tried to access a variable as an array.
However, that variable /is not/ an array; it is a boolean
value. Thus, the array access failed.

From your code snippet above, I suspect that line 104 looks like
$a = $b['something'];
The error message is trying to tell you that
$b
is not an array (and helpfully tells you that it /is/ a boolean),
so it cannot resolve the array access
$b['something']

As to /why/ the variable is /not/ an array, you will have to determine
that for yourself.

With regards to the code snippet you attached here, I CAN tell you
that the PDO fetch() method returns an array on success, OR the
boolean value FALSE on failure. Perhaps you need take a look at
the code that initializes the suspect array named in line 104,
to see if the initializer does something similar.

HTH
--
Lew Pitcher
"In Skills, We Trust"

Re: Help With Warning Message

<8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=342&group=comp.lang.php#342

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc3.netnews.com!feeder5.feed.usenet.farm!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.iad.POSTED!not-for-mail
From: noemail@noemail.com (Call Me Tom)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Message-ID: <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com> <sfuut5$lb8$1@dont-email.me>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 53
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Mon, 23 Aug 2021 05:04:31 UTC
Date: Mon, 23 Aug 2021 01:03:04 -0400
X-Received-Bytes: 2507
 by: Call Me Tom - Mon, 23 Aug 2021 05:03 UTC

$da_name = $da['airport_name']; is line 104. When I Access $da_name
in my table, it prints the airport name. Can you tell what it thinks
is a boolean?
Tom

On Mon, 23 Aug 2021 01:50:29 -0000 (UTC), Lew Pitcher
<lew.pitcher@digitalfreehold.ca> wrote:

>On Sun, 22 Aug 2021 20:00:04 -0400, Call Me Tom wrote:
>
>> 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
>>
>> I don't even know where to begin.
>
>Start with looking at line 104 of the PHP code in file
>C:\xampp\htdocs\CAA_Tom\logbook.php
>
>> What is the warning trying to tell me?
>
>On line 104, you tried to access a variable as an array.
>However, that variable /is not/ an array; it is a boolean
>value. Thus, the array access failed.
>
>From your code snippet above, I suspect that line 104 looks like
> $a = $b['something'];
>The error message is trying to tell you that
> $b
>is not an array (and helpfully tells you that it /is/ a boolean),
>so it cannot resolve the array access
> $b['something']
>
>As to /why/ the variable is /not/ an array, you will have to determine
>that for yourself.
>
>With regards to the code snippet you attached here, I CAN tell you
>that the PDO fetch() method returns an array on success, OR the
>boolean value FALSE on failure. Perhaps you need take a look at
>the code that initializes the suspect array named in line 104,
>to see if the initializer does something similar.
>
>HTH

Re: Help With Warning Message

<ioh040Fpg0kU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=343&group=comp.lang.php#343

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: user@example.net (J.O. Aho)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Date: Mon, 23 Aug 2021 09:13:04 +0200
Lines: 28
Message-ID: <ioh040Fpg0kU1@mid.individual.net>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
<sfuut5$lb8$1@dont-email.me> <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 4hsi89RuCwKCflOduDj2iAvz6r7/mLfcI+fX0dkYRCaAs+sKrs
Cancel-Lock: sha1:O8FrrN5Uy5WsH+ZKeTq1SfaRo48=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
In-Reply-To: <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
Content-Language: en-US-large
 by: J.O. Aho - Mon, 23 Aug 2021 07:13 UTC

On 23/08/2021 07.03, Call Me Tom wrote:
>
> $da_name = $da['airport_name']; is line 104. When I Access $da_name
> in my table, it prints the airport name. Can you tell what it thinks
> is a boolean?

The $da is boolean for the row before failed:

$da = $result4->FETCH(PDO::FETCH_ASSOC);

So you should check the $da value before using it

if($da) {
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
} else {
// do what you need to tell it didn't find any thing
}

also worth mentioning, the variable $origin needs to be sanitized, as
it's a prone for SQL-injection. I do suggest you use withe list method,
where you check that the string has only allowed characters.

--

//Aho

Re: Help With Warning Message

<sg01rp$a86$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=344&group=comp.lang.php#344

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Date: Mon, 23 Aug 2021 11:47:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <sg01rp$a86$1@dont-email.me>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
<sfuut5$lb8$1@dont-email.me> <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 23 Aug 2021 11:47:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="feff7aca22ce181f8c076f8cf1e2a6ee";
logging-data="10502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19P0LSw66lA5cmakRaKf7sidkYoj5XS8x0="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:Jh1+ffQXccj2EuVXohRePP7gPvA=
 by: Lew Pitcher - Mon, 23 Aug 2021 11:47 UTC

On Mon, 23 Aug 2021 01:03:04 -0400, Call Me Tom wrote:

> $da_name = $da['airport_name']; is line 104. When I Access $da_name
> in my table, it prints the airport name. Can you tell what it thinks
> is a boolean?

[snip

> On Mon, 23 Aug 2021 01:50:29 -0000 (UTC), Lew Pitcher
> <lew.pitcher@digitalfreehold.ca> wrote:

>>With regards to the code snippet you attached here, I CAN tell you
>>that the PDO fetch() method returns an array on success, OR the
>>boolean value FALSE on failure. Perhaps you need take a look at
>>the code that initializes the suspect array named in line 104,
>>to see if the initializer does something similar.

--
Lew Pitcher
"In Skills, We Trust"

Re: Help With Warning Message

<sg0oqs$g6i$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=345&group=comp.lang.php#345

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Date: Mon, 23 Aug 2021 18:19:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <sg0oqs$g6i$1@dont-email.me>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
<sfuut5$lb8$1@dont-email.me> <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
<ioh040Fpg0kU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 23 Aug 2021 18:19:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="feff7aca22ce181f8c076f8cf1e2a6ee";
logging-data="16594"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8bUM/3x5atcoUEEyPzsYJjVxnDnkDn6Q="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:Jq7sJI+bNgqip4XQKwGBnJJnxto=
 by: Lew Pitcher - Mon, 23 Aug 2021 18:19 UTC

On Mon, 23 Aug 2021 09:13:04 +0200, J.O. Aho wrote:

> On 23/08/2021 07.03, Call Me Tom wrote:
>>
>> $da_name = $da['airport_name']; is line 104. When I Access $da_name
>> in my table, it prints the airport name. Can you tell what it thinks
>> is a boolean?
>
> The $da is boolean for the row before failed:
>
> $da = $result4->FETCH(PDO::FETCH_ASSOC);
>
> So you should check the $da value before using it
>
> if($da) {
> $da = $result4->FETCH(PDO::FETCH_ASSOC);
> $da_name = $da['airport_name'];
> } else {
> // do what you need to tell it didn't find any thing
> }
ITYM
$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
/* fetch() failed for some reason. abort this activity */
}
else $da_name = $da['airport_name'];

And, to be complete, the OP should check the value in $result4
before invoking the fetch() method. So the logic fragment should
look more like
$result4 = $dbh->query($query4);
if ($result4 === FALSE)
{
/* query() failed for some reason - abort this activity */
}
else
{
$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
/* fetch() failed for some reason - abort this activity */
}
else $da_name = $da['airport_name'];
}

> also worth mentioning, the variable $origin needs to be sanitized, as
> it's a prone for SQL-injection. I do suggest you use withe list method,
> where you check that the string has only allowed characters.

--
Lew Pitcher
"In Skills, We Trust"

Re: Help With Warning Message

<uss7ig5tktjv42so9n5sl6gagsbp60hrcr@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=346&group=comp.lang.php#346

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx45.iad.POSTED!not-for-mail
From: noemail@noemail.com (Call Me Tom)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Message-ID: <uss7ig5tktjv42so9n5sl6gagsbp60hrcr@4ax.com>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com> <sfuut5$lb8$1@dont-email.me> <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com> <ioh040Fpg0kU1@mid.individual.net> <sg0oqs$g6i$1@dont-email.me>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 59
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Mon, 23 Aug 2021 19:28:29 UTC
Date: Mon, 23 Aug 2021 15:26:58 -0400
X-Received-Bytes: 2495
 by: Call Me Tom - Mon, 23 Aug 2021 19:26 UTC

Thank yopu for your help. I found the problem
Tom.

On Mon, 23 Aug 2021 18:19:08 -0000 (UTC), Lew Pitcher
<lew.pitcher@digitalfreehold.ca> wrote:

>On Mon, 23 Aug 2021 09:13:04 +0200, J.O. Aho wrote:
>
>> On 23/08/2021 07.03, Call Me Tom wrote:
>>>
>>> $da_name = $da['airport_name']; is line 104. When I Access $da_name
>>> in my table, it prints the airport name. Can you tell what it thinks
>>> is a boolean?
>>
>> The $da is boolean for the row before failed:
>>
>> $da = $result4->FETCH(PDO::FETCH_ASSOC);
>>
>> So you should check the $da value before using it
>>
>> if($da) {
>> $da = $result4->FETCH(PDO::FETCH_ASSOC);
>> $da_name = $da['airport_name'];
>> } else {
>> // do what you need to tell it didn't find any thing
>> }
>
>ITYM
> $da = $result4->FETCH(PDO::FETCH_ASSOC);
> if ($da === FALSE)
> {
> /* fetch() failed for some reason. abort this activity */
> }
> else $da_name = $da['airport_name'];
>
>And, to be complete, the OP should check the value in $result4
>before invoking the fetch() method. So the logic fragment should
>look more like
> $result4 = $dbh->query($query4);
> if ($result4 === FALSE)
> {
> /* query() failed for some reason - abort this activity */
> }
> else
> {
> $da = $result4->FETCH(PDO::FETCH_ASSOC);
> if ($da === FALSE)
> {
> /* fetch() failed for some reason - abort this activity */
> }
> else $da_name = $da['airport_name'];
> }
>
>
>> also worth mentioning, the variable $origin needs to be sanitized, as
>> it's a prone for SQL-injection. I do suggest you use withe list method,
>> where you check that the string has only allowed characters.

Re: Help With Warning Message

<ioim27F5698U1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=347&group=comp.lang.php#347

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news-2.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: user@example.net (J.O. Aho)
Newsgroups: comp.lang.php
Subject: Re: Help With Warning Message
Date: Tue, 24 Aug 2021 00:33:43 +0200
Lines: 44
Message-ID: <ioim27F5698U1@mid.individual.net>
References: <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
<sfuut5$lb8$1@dont-email.me> <8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
<ioh040Fpg0kU1@mid.individual.net> <sg0oqs$g6i$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net Cl7FaIe7w8Xykn70mnqIeA2ZCLLgsm5H9pT2tptqyeSh7sY353
Cancel-Lock: sha1:5wjEFYcaM3RuUcYEo0eLQ3ri6vk=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
In-Reply-To: <sg0oqs$g6i$1@dont-email.me>
Content-Language: en-US-large
 by: J.O. Aho - Mon, 23 Aug 2021 22:33 UTC

On 23/08/2021 20.19, Lew Pitcher wrote:

> And, to be complete, the OP should check the value in $result4
> before invoking the fetch() method. So the logic fragment should
> look more like
> $result4 = $dbh->query($query4);
> if ($result4 === FALSE)
> {
> /* query() failed for some reason - abort this activity */
> }
> else
> {
> $da = $result4->FETCH(PDO::FETCH_ASSOC);
> if ($da === FALSE)
> {
> /* fetch() failed for some reason - abort this activity */
> }
> else $da_name = $da['airport_name'];
> }
>
Make the code even better (and more readable), we should skip the else-part

$result4 = $dbh->query($query4);
if ($result4 === FALSE)
{
throw new Exception("Query failed: " + $dbh->errorInfo()[2]);
}

$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
throw new Exception("Fetch failed.");
}

$da_name = $da['airport_name'];

Of course this requires you take care of the exception higher up in the
code and give a nice general error message, log the exception.

--

//Aho

Re: Help With Warning Message

<ip3inoFdl1gU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=352&group=comp.lang.php#352

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: usenet@arnowelzel.de (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: <ip3inoFdl1gU1@mid.individual.net>
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>
 by: Arno Welzel - Mon, 30 Aug 2021 08:21 UTC

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:

<https://www.php.net/manual/en/pdostatement.fetch.php>

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:

<https://www.php.net/manual/en/pdostatement.execute.php>

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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor