Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18771 > unrolled thread

Help With Warning Message

Started byCall Me Tom <noemail@noemail.com>
First post2021-08-22 20:00 -0400
Last post2021-08-30 10:21 +0200
Articles 9 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-22 20:00 -0400
    Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 01:50 +0000
      Re: Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-23 01:03 -0400
        Re: Help With Warning Message "J.O. Aho" <user@example.net> - 2021-08-23 09:13 +0200
          Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 18:19 +0000
            Re: Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-23 15:26 -0400
            Re: Help With Warning Message "J.O. Aho" <user@example.net> - 2021-08-24 00:33 +0200
        Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 11:47 +0000
    Re: Help With Warning Message Arno Welzel <usenet@arnowelzel.de> - 2021-08-30 10:21 +0200

#18771 — Help With Warning Message

FromCall Me Tom <noemail@noemail.com>
Date2021-08-22 20:00 -0400
SubjectHelp With Warning Message
Message-ID<7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com>
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

[toc] | [next] | [standalone]


#18772

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-08-23 01:50 +0000
Message-ID<sfuut5$lb8$1@dont-email.me>
In reply to#18771
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"

[toc] | [prev] | [next] | [standalone]


#18773

FromCall Me Tom <noemail@noemail.com>
Date2021-08-23 01:03 -0400
Message-ID<8ia6ig94heh1gjujblafvn17or10ufdlik@4ax.com>
In reply to#18772
  $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

[toc] | [prev] | [next] | [standalone]


#18774

From"J.O. Aho" <user@example.net>
Date2021-08-23 09:13 +0200
Message-ID<ioh040Fpg0kU1@mid.individual.net>
In reply to#18773
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

[toc] | [prev] | [next] | [standalone]


#18776

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-08-23 18:19 +0000
Message-ID<sg0oqs$g6i$1@dont-email.me>
In reply to#18774
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"

[toc] | [prev] | [next] | [standalone]


#18777

FromCall Me Tom <noemail@noemail.com>
Date2021-08-23 15:26 -0400
Message-ID<uss7ig5tktjv42so9n5sl6gagsbp60hrcr@4ax.com>
In reply to#18776
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.

[toc] | [prev] | [next] | [standalone]


#18778

From"J.O. Aho" <user@example.net>
Date2021-08-24 00:33 +0200
Message-ID<ioim27F5698U1@mid.individual.net>
In reply to#18776
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

[toc] | [prev] | [next] | [standalone]


#18775

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-08-23 11:47 +0000
Message-ID<sg01rp$a86$1@dont-email.me>
In reply to#18773
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"

[toc] | [prev] | [next] | [standalone]


#18783

FromArno Welzel <usenet@arnowelzel.de>
Date2021-08-30 10:21 +0200
Message-ID<ip3inoFdl1gU1@mid.individual.net>
In reply to#18771
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web